modifying specific lines a file

Hi
I need to modify entire file starting from the 3 line. file looks like this
filename : exp

first line
second
1,"wes","est","ws"
1,"was","qwe","qwa"

also i have to replace the third content from 3 line by a counter . so the output should look like this:

first line
second
1,"wes","1","ws"
1,"was","2","qwa"

here 1, 2 is a type of incrementing counter ...

please help :frowning:

try..

awk -F, '{x=x+1} {print $1","$2","x","$4}' file
nawk 'BEGIN{n=1;OFS=FS=","}NR>=3{$3=sprintf("\"%s\"",n);n++;print;}'

awk -F, '{x=x+1} {print $1","$2","x","$4}'

This gives output :
first line,,1, second,,2,
1,"wes",3,"ws"
1,"was",4,"qwa"
,,5,

it alters first 2 lines as well...

---------- Post updated at 05:42 AM ---------- Previous update was at 05:38 AM ----------

the first 2 lines got missed out ...

1,"wes","1","ws"
1,"was","2","qwa"
,,"3"

:frowning:

i have tried replacing the 3 column with sed and awk .. its working ...
my problem boils down to

how to copy first 2 lines of a file and only alter the remaining lines 3rd column ? i have to store this modified file in some other file ....

This should do the trick:

awk -F, 'NR>2{$3="\042"++c"\042"}1' OFS="," file

will this work?

awk -F, 'NR<3 {print $0}; NR>=3 {x=x+1; print $1","$2","x","$4}' <file>

THIS IS PRINTING AN ADDITIONAL THIRD LINE ...

first line
second
1,"wes",1,"ws"
1,"was",2,"qwa"
,,3,

i think it will work .. but can make me understand the logic ?

---------- Post updated at 06:01 AM ---------- Previous update was at 05:59 AM ----------

also the file i m using , i m not certain of the number of fields it will have ... i mean it is not necessarily 4 ...

Shorter :wink:

awk '{FS=OFS=","}NR>2{$3="\""++c"\""}1' file

awk -F, 'NR>2{$3="\042"++c"\042"}1' OFS="," file

this is too giving me an additional line first line
second
1,"wes","1","ws"
1,"was","2","qwa"
,,"3"

in the end ...

---------- Post updated at 06:06 AM ---------- Previous update was at 06:04 AM ----------

awk '{FS=OFS=","}NR>2{$3="\""++c"\""}1' file

again the same probs ...
first line
second
1,"wes","1","ws"
1,"was","2","qwa"
,,"3"

:frowning:

Is it possible that you have a blank line at the end of your file?

the input file cannot be changed ::frowning:

Try this:

awk '! NF{next}{FS=OFS=","}NR>2{$3="\""++c"\""}1' 

To keep the forums high quality for all users, please take the time to format your posts correctly.

Use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Thank You.

The UNIX and Linux Forums

thanks it works ...

:slight_smile: