0

I want to replace the text sequentially in a file.

The file's contents are:

1,01-JUN-2016,ABC
2,01-JUN-2016,ABC
3,01-JUN-2016,ABC
4,01-JUN-2016,ABC
5,01-JUN-2016,ABC
6,02-JUN-2016,ABC
7,02-JUN-2016,ABC
8,02-JUN-2016,ABC
9,02-JUN-2016,ABC
10,02-JUN-2016,ABC
11,02-JUN-2016,ABC
12,02-JUN-2016,ABC
13,02-JUN-2016,ABC
14,02-JUN-2016,ABC

I want this to be modified as below:

1,01-JUN-2016,ABC1
2,01-JUN-2016,ABC2
3,01-JUN-2016,ABC3
4,01-JUN-2016,ABC4
5,01-JUN-2016,ABC5
6,02-JUN-2016,ABC6
7,02-JUN-2016,ABC7
8,02-JUN-2016,ABC1
9,02-JUN-2016,ABC2
10,02-JUN-2016,ABC3
11,02-JUN-2016,ABC4
12,02-JUN-2016,ABC5
13,02-JUN-2016,ABC6
14,02-JUN-2016,ABC7
1

using awk

awk '$0=$0 (NR-1)%7+1' file

or

awk '{print (NR-1)%7+1}' file

Basically the subtract 1 is to start on the number zero.
The modulo 7 is loop back every seven lines.
The plus 1 is due to the fact the 7 mod 7 is 0 which is not what we want to start on, we want to start on 1 so we +1.

  • where does the OP say they want to use the record number and not the first field? – cas Jun 8 '16 at 7:49
0
$ awk -F, 'FNR < 8 { print $0$1 }; FNR > 7 {print $0($1-7)}' arvind.txt 
1,01-JUN-2016,ABC1
2,01-JUN-2016,ABC2
3,01-JUN-2016,ABC3
4,01-JUN-2016,ABC4
5,01-JUN-2016,ABC5
6,02-JUN-2016,ABC6
7,02-JUN-2016,ABC7
8,02-JUN-2016,ABC1
9,02-JUN-2016,ABC2
10,02-JUN-2016,ABC3
11,02-JUN-2016,ABC4
12,02-JUN-2016,ABC5
13,02-JUN-2016,ABC6
14,02-JUN-2016,ABC7

For line numbers < 8, print the entire line followed by the first field. For line numbers > 7, print the entire line followed by the value of the first field minus 7.

Note, FNR is the number of input records that awk has seen in the current input file so far. FNR is an awk built-in variable. It has nothing (beyond co-incidence) to do with the values you have in the first field of your input file.

BTW, if you run this with multiple input files and you want a cumulative line-count for all input files (rather than a separate line count for each file), use NR rather than FNR.

  • Why are you using $1 instead of FNR in the print statements? Where does OP say they want to use the Values from the first field? – 123 Jun 8 '16 at 7:09
  • where do they say they don't? Either FNR or $1 work here because they're (coincidentally) the same value for the provided sample input. – cas Jun 8 '16 at 7:41
  • They don't say either, but I didn't assume they cared about either. As far as i can tell from the limited information they just wanted to add 1-7 repeatedly to the end of the last field. FNR is always going to be correct to produce this pattern, there is a possibility that $1 won't be. – 123 Jun 8 '16 at 7:58
  • exactly my point. and AFAICT they wanted the value of $1 or $1 - 7 (or even ($1-1) % 7 +1) added to the end of each line, and using FNR isn't always going to be correct (It'll only be correct where FNR==$1). I probably should have used awk '{print $0(($1 - 1) % 7 +1)}' arvind.txt – cas Jun 8 '16 at 8:09
  • Thanks for your quick inputs. I need this only for 1 file so will go with your solution. – Arvind Jun 8 '16 at 8:17
0

Try this:

awk -F, '$1 <=7 { print $0$1 }; $1 > 7 {print $0($1-7)}' filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.