0

I want to modify the following script:

awk 'NR>242 && $1 =='$t' {print $4, "\t" '$t'}' test.txt > file

I want to add a condition for the first "1 to 121" data (corresponding to the first 121 points) and then for the "122 to 242" data (which corresponds to the other 121 points).

so it becomes:

when NR>242 take the corresponding values of rows form 1 to 121 print them to file1

when NR>242 take the corresponding values of rows form 121 to 242 print them to file2

Thanks!

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • When you read row 243, (so first row for which NR > 242), you no longer have access to row 1 (unless you've stored it). Are you using the word "row" when you mean "column"? – William Pursell Jun 15 '21 at 16:31
  • The data files stores the points first and then the corresponding data for all the points regarding the condition statement, it is as follows: when NR>242 && NR are from 1 to 121, take the corresponding values from column 4 and print them to file1 when NR>242 && NR are from 121 to 242, take the corresponding values from column 4 and print them to file2 I hope it is more clear now, the thing is that every 242 rows after row number 242 are asscoiated with specific time step, and are divided into two regions, 1 to 121, and 121 to 242. Thanks again. – Hussein Kokash Jun 15 '21 at 17:27
  • "when NR > 242 && NR are from 1 to 121". What does that mean? If NR > 242, then NR is not between 1 and 121. Do you mean "NR > 242 && 0 < NF < 122"? ie, do you mean NF vs. NR? – William Pursell Jun 15 '21 at 17:41
  • @HusseinKokash, IMHO in spite of explaining from code wise(which you did show your efforts) you could post samples of input and expected ouytout in your question. Keep samples simple and short so that we could test our code in it, thank you. – RavinderSingh13 Jun 15 '21 at 17:48
  • I am sorry if my statement was misleading William, What I meant that after row number 242 I have 242 data (rows) where they are grouped as this: the 1st 121 data should go to file1 and the last 121 data should go to file2, so the count begin at row number 243 up to 484 if I want to make it less confusing. Thats is why I want to say if NR is > 242 I want the first 121 rows to go to file1, and the last 121 rows to go to file2 Thanks again and excuse my lack of experience – Hussein Kokash Jun 15 '21 at 18:01
  • `'$t'` is the wrong way to access a shell variables value inside an awk script. See https://stackoverflow.com/questions/19075671/how-do-i-use-shell-variables-in-an-awk-script for the right ways to do that. [edit] your question to include a [mcve] with concise, testable sample input and expected output so we can help you. Obviously don't create an example with 242+ rows - 10 rows will be more than enough to demonstrate your problem. – Ed Morton Jun 15 '21 at 19:13
  • Nothing in your question matches your subject line of `Add a condtion for specfic row length in a script` - row length is never mentioned again. – Ed Morton Jun 15 '21 at 19:22

2 Answers2

2

Generic solution: Adding more generic solution here, where you could give all line numbers inside lines variable of awk program. Once line number matches with values it will increase counter of file with 1 eg: from file1 to file2 OR file2 to file3 and so on...

awk -v val="$t" -v lines="121,242" -v count=1'
BEGIN{
num=split(lines,arr,",")
for(i=1;i<=num;i++){
  line[arr[i]]
  outputfile="file"count
}
}
FNR in arr[i]{
  close(outputfile)
  outputfile="file"++count
}
($1 == val){
  print $4 "\t" val > (outputfile)
}
' Input_file


With your shown samples, please try following. This will print all lines from 1st line to 242nd line to file1 and 243 line onwards it will print output to file2. Also program has a shell variable named t passed into awk program's variable named val here.

awk -v val="$t" '
FNR==1{
  outputfile="file1"
}
FNR==243{
  outputfile="file2"
}
($1 == val){
  print $4 "\t" val > (outputfile)
}
' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thank you so much for your valuable input. regarding the condition statement, it is as follows: when NR>242 && NR are from 1 to 121, take the corresponding values from column 4 and print them to file1 when NR>242 && NR are from 121 to 242, take the corresponding values from column 4 and print them to file2 I hope it is more clear now, the thing is that every 242 rows after row number 242 are asscoiated with specific time step, and are divided into two regions, 1 to 121, and 121 to 242. Thanks again, appreciate your assistance – Hussein Kokash Jun 15 '21 at 17:25
  • @HusseinKokash, that's what my answer supposed to do. If line is greater than 242 and 1st column is equal to your shell variable then it prints that line to file2. Let me know once you test my code – RavinderSingh13 Jun 15 '21 at 17:32
  • @HusseinKokash, Please try my Generic solution once(with reading its all description; how its work; explanation given in answer itself) and let me know how it goes, cheers. – RavinderSingh13 Jun 15 '21 at 18:07
0
$ awk -v val="$t" '{c=int((NR-1)%242/121)+1}
           $1==val {print $4 "\t" $1 > (output"c")}' file

this should take the first, third, etc blocks of 121 records to output1 and second, fourth, etc blocks of 121 records to output2 if they satisfy the condition.

If you want to skip first two blocks (first 242 records) just add && NR>242 condition to the existing one.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Hello Karakfa, I have a text file where the first 242 lines are points coordinates, 121 make a line and another 121 make another line, and after line 242 are the results, for each time there is 242 results, 121 for the first line and the next 121 for the other one, the code I wrote is for only 121 points (one line) and after line 121 there are 121 results for each time, now I have added another 121 points and they are reflected on the results, I want to modify the line so that for each time it take the first 121 lines (after line 242) to a file and the next 121 to another file for each time – Hussein Kokash Jun 17 '21 at 16:27
  • Did you try the script with the modification mentioned? Is it not what you specified? – karakfa Jun 17 '21 at 16:41
  • It didn't! Thanks anyway – Hussein Kokash Jun 17 '21 at 17:21