Visit Our UNIX and Linux User Community


modifying specific lines a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modifying specific lines a file
# 1  
Old 07-30-2009
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 Smilie
# 2  
Old 07-30-2009
try..
Code:
awk -F, '{x=x+1} {print $1","$2","x","$4}' file

# 3  
Old 07-30-2009
Code:
nawk 'BEGIN{n=1;OFS=FS=","}NR>=3{$3=sprintf("\"%s\"",n);n++;print;}'

# 4  
Old 07-30-2009
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 ----------

Quote:
Originally Posted by summer_cherry
Code:
nawk 'BEGIN{n=1;OFS=FS=","}NR>=3{$3=sprintf("\"%s\"",n);n++;print;}'


the first 2 lines got missed out ...

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

Smilie

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 ....
# 5  
Old 07-30-2009
This should do the trick:

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

# 6  
Old 07-30-2009
will this work?

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

# 7  
Old 07-30-2009
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 ...

Previous Thread | Next Thread
Test Your Knowledge in Computers #125
Difficulty: Easy
UNIX is a certification of a complete OS system, not only a UNIX kernel.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

2. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

3. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

4. Solaris

Setting/Modifying variable specific to target in Makefile

Hi, I have a small piece of Makefile script which throw's error on Sun Sparc machine, but works fine with Sun Optron, Linux, AIX machines. FOO=Naveen test1:FOO=Dhilip test1: @echo FOO is ${FOO} test2: @echo Me is ${FOO} Output on Sun Sparc - ukhml-v890new-~/test: make test1... (5 Replies)
Discussion started by: nsriram
5 Replies

5. Shell Programming and Scripting

awk modifying entries on 2 lines at 2 positions

Hi this script adds text in the correct place on one line only, in a script. awk 'BEGIN{ printf "Enter residue and chain information: " getline var < "-" split(var,a) } /-s rec:/{$7=a; } {print}' FLXDOCK but I need the same info added at position 7 on line 34 and... (1 Reply)
Discussion started by: gav2251
1 Replies

6. Shell Programming and Scripting

Substitute specific lines with lines from another file

Hello All, I am new to this forum. I am currently facing a problem in manipulating files. I have two files called old-matter and new-matter # cat old-matter abc: this, is a, sample, entry byi: white board, is white in color rtz: black, board is black qty: i tried, a lot asd: no... (1 Reply)
Discussion started by: rahmathulla
1 Replies

7. UNIX for Dummies Questions & Answers

how to display specific lines of a specific file

are there any basic commands that can display lines 99 - 101 of the /etc/passwd file? I'm thinking use of head and tail, but I forget what numbers to use and where to put /etc/passwd in the command. (2 Replies)
Discussion started by: raidkridley
2 Replies

8. UNIX for Dummies Questions & Answers

Displaying specific lines in a file.

I'm trying to figure out how to display a certain line in a text file. I keep getting references to Tail and Head, and I know how these work, but i'm lost on how to find say the third out of the five lines and display only that. I thought maybe grep could help, but that doesn't seem likely. ... (3 Replies)
Discussion started by: MaestroRage
3 Replies

9. UNIX for Dummies Questions & Answers

How do you specific lines in a file?

I'm looking to show specific lines (eg. 20 through 40) of a series of files in the directory. How do I do this? (2 Replies)
Discussion started by: hedgehog001
2 Replies

10. Shell Programming and Scripting

Deleting specific lines in a file

I have a file which has some lines starting with a particular word. I would like to delete 5 lines before each such line containing that particular word. eg: line1 line2 line3 line4 line5 line6 "particular word"... I would like to delete line2-line6 and all such occurences in that... (4 Replies)
Discussion started by: ramu_1980
4 Replies

Featured Tech Videos