Modify text file using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modify text file using shell script
# 1  
Old 06-01-2011
Modify text file using shell script

Hi,

I have a text file which is following format -

COL VAL
ABC 1
ABC 2
ABC 3
ABC 4
ABC 5

My requirement is to search for a particular value (provided by user) in the file and comment the previous entries including that as well.

E.g. If I search for number 3, then the output file should be similar to -

COL VAL
#ABC 1
#ABC 2
#ABC 3
ABC 4
ABC 5

Kindly suggest with some options. Thanks for your time.
# 2  
Old 06-01-2011
if 'val' is a unique field.

Code:
kent$  echo "ABC 1
ABC 2
ABC 3
ABC 4
ABC 5"|awk 'BEGIN{a=1}{print (a)? "#"$0:$0; if($2==3)a=0}'
#ABC 1
#ABC 2
#ABC 3
ABC 4
ABC 5

# 3  
Old 06-01-2011
However if the values are not unique and not sorted, you can use the following:

Code:
echo "ABC 1
ABC 2
ABC 3
ABC 4
ABC 5" | awk '{if ($2 <= 3) {print "#"$0} else {print $0}}'

Works as well with:

Code:
echo "ABC 1
ABC 2
ABC 3
ABC 4
ABC 5
ABC 4
ABC 2" | awk '{if ($2 <= 3) {print "#"$0 } else {print $0}}'

# 4  
Old 06-01-2011
Code:
$ echo "ABC 1
ABC 2
ABC 3
ABC 4
ABC 5
ABC 4
ABC 2" | sed '/ [123]$/s/.*/#&/'
#ABC 1
#ABC 2
#ABC 3
ABC 4
ABC 5
ABC 4
#ABC 2

# 5  
Old 06-01-2011
Quote:
Originally Posted by ctsgnb
Code:
$ echo "ABC 1
ABC 2
ABC 3
ABC 4
ABC 5
ABC 4
ABC 2" | sed '/ [123]$/s/.*/#&/'
#ABC 1
#ABC 2
#ABC 3
ABC 4
ABC 5
ABC 4
#ABC 2

Doesn't work with values greater than 10 though. If only sed supported if statements ...
# 6  
Old 06-01-2011
which range of value do you need to match ?

or which values do you need to match ?

---------- Post updated at 07:27 PM ---------- Previous update was at 07:14 PM ----------

Code:
$ cat tst
ABC 1
ABC 2
ABC 3
ABC 4
ABC 5
ABC 1
ABC 6
ABC 2
ABC 3
ABC 4
ABC 5
ABC 6

Code:
$ a=3
$ nawk -v N="$a" '$NF<=N{printf "#"}1' tst
#ABC 1
#ABC 2
#ABC 3
ABC 4
ABC 5
#ABC 1
ABC 6
#ABC 2
#ABC 3
ABC 4
ABC 5
ABC 6

This User Gave Thanks to ctsgnb For This Post:
# 7  
Old 06-04-2011
Thank you everyone for responding to my question. Specially to "ctsgnb" as the solution he provided worked perfectly in my scenario.

Thanks again :-)




Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modify text file using sed

Hello all, I have some text files I need to do the following on: Delete banner page (lines 1-56) --I am doing this using sed Remove ^M --I am doing this using vi Remove trailer page --this can vary based on the contents of the file, it usually starts with *************************** I am... (5 Replies)
Discussion started by: jeffs42885
5 Replies

2. Shell Programming and Scripting

Modify text file using awk

I have text file with lines as shown here. Each row has 11 columns separated by tab. In each row, i want to split the 8th column such that the output should look like shown below. Here value in the 9th column is DP value and in the 10th column is MQ value followed by the values after resource.EFF=.... (15 Replies)
Discussion started by: mehar
15 Replies

3. Shell Programming and Scripting

Modify the text file by script

Hi All the Helpers! I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt input.txt VOP 111 0 1 2 DEM 111 0 222 333 444 555 879 888 987 888 989 VOP 118 0... (2 Replies)
Discussion started by: Indra2011
2 Replies

4. Shell Programming and Scripting

Shell script to modify file in several directories

Hi, I want a script shell to automate modifying httpd.conf file for several instances of apache, save httpd.file before changing it, after modifying it and then restart apache. - Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories : "... (4 Replies)
Discussion started by: bras39
4 Replies

5. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

6. UNIX for Dummies Questions & Answers

Modify Text File

Hi, I would like to remove any lines from a text file that begin with #, or that are blank. How can I do that with BASH? Mike (3 Replies)
Discussion started by: msb65
3 Replies

7. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

how to modify a file using shell script

Hi, i am using SuonOS and ksh. i need to add data into a file(s.txt) using a shell script. i have to pass 3 parameters and these 3 paramaters should add into the file at end of the file. File s.txt is look like, --------------------------------- column1|column2|column3 ... (1 Reply)
Discussion started by: syamkp
1 Replies

10. Shell Programming and Scripting

Modify a text or xml file

Hi all, I want to write a shell which would allow me to edit a text file or a xml file. Basically i want to add a new node in a existing xml file. The values for this new node are based on user input. Thanks in advance Zing (9 Replies)
Discussion started by: zing
9 Replies
Login or Register to Ask a Question