SED/AWK file read & manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED/AWK file read & manipulation
# 1  
Old 03-10-2010
SED/AWK file read & manipulation

I have large number of data files, close to 300 files, lets say all files are same kind and have extension .dat , each file have mulitple lines in it.

There is a unique line in each file containing string 'SERVER'. Right after this line there is another line which contain a string 'DIGIT=0', however this line 'DIGIT=0' appears at other places within the file. I need to know how I can change this 'DIGIT=0' to 'DIGIT=1' only where it appears after the line containing the string 'SERVER' and not at other places in the file. All the lines needs to be redirected to another file if the change is not possible on the original file.

Example of cat mydata.dat file:

APP1
APP3
DIGIT=2
DIGIT=1
DIGIT=0
DIGIT=0
APP3
APP2
SERVER
DIGIT=0 <----- I need to change this DIGIT=0 to DIGIT=1
DIGIT=1
APP2

----------------------------------------------------------
If I use sed then it changes DIGIT=0 at other places
for example: sed -e 's/DIGIT=0/DIGIT=1/' file > newfile

I am not that shell script guru so need help from you all, I think it should be simple but I will learn some thing new.

Thanks a lot.
# 2  
Old 03-10-2010
Hi, sal_tx:
Code:
sed '/SERVER/{n;/^DIGIT=0/s/0/1/;}' file

Regards,
Alister
# 3  
Old 03-10-2010
Code:
awk '/SERVER/ {print;getline;gsub(/DIGIT=0/,"DIGIT=1",$0)}1' urfile

# 4  
Old 03-10-2010
Try:

Code:
perl -i  -lne '$f=1 if (/SERVER/);  if ($f && (/DIGIT=0/)) {print "DIGIT=1"; $f=0; } else { print };'  *.dat

# 5  
Old 03-11-2010
Thanks all, I just received the code from Allister, tried the sed command and it worked.

I will also try awk script & perl also, these will be handy scripts.

My best regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Text manipulation with sed/awk in a bash script

Guys, I have a variable in a script that I want to transform to into something else Im hoping you guys can help. It doesn't have to use sed/awk but I figured these would be the simplest. DATE=20160120 I'd like to transform $DATE into "01-20-16" and move it into a new variable called... (8 Replies)
Discussion started by: dendenyc
8 Replies

2. Shell Programming and Scripting

Replacing FQDN by hostnames in a CSV file with sed & awk

Hello, Beginning with shell scipting, I'm trying to find in a csv file, the lines where the field related to hostname is displayed as an FQDN intead the hostname. (some lines are correct) and the to correct that inside the file: Novell,11.0,UNIX Server,bscpsiws02,TxffnX1tX1HiDoyBerrzWA==... (2 Replies)
Discussion started by: Wonto
2 Replies

3. Shell Programming and Scripting

Want to sort a file using awk & sed to get required output

Hi All, Need Suggestion, Want to sort a file using awk & sed to get required, output as below, such that each LUN shows correct WWPN and FA port Numbers correctly: Required output: 01FB 10000000c97843a2 8C 0 01FB 10000000c96fb279 9C 0 22AF 10000000c97843a2 8C 0 22AF 10000000c975adbd ... (10 Replies)
Discussion started by: aix_admin_007
10 Replies

4. Shell Programming and Scripting

'Couldn't read file' error in bash script with expect, sed and awk!

Ok, so I have a bash script with an embedded expect statement. Inside of the expect statement, i'm trying to pull all of the non-comment lines from the /etc/oratab file one at a time. Here's my command: cat /etc/oratab |sed /^s*#/d\ | awk 'NR==1'|awk -F: '{print \"$1\"}'|. oraenv Now,... (0 Replies)
Discussion started by: alexdglover
0 Replies

5. UNIX for Dummies Questions & Answers

Line & File Manipulation - add spaces between characters

Is there an awk, sed, vi or any line command that adds Field Separators (default spaces) to each line in a file? $cat RegionalData 12FC2525MZLP8266900216 12FC2525MZLP8266900216 12FC2525NBLP8276900216 12FC2525NBLP8276900216 Desired results: 1 2 F C 2525 MZ LP 826 690 02 16 1 2 F C... (2 Replies)
Discussion started by: MS75001
2 Replies

6. Shell Programming and Scripting

File manipulation with AWK and SED

Hello How do i check that correct input files are used while using AWk and SED for file manipulation? e.g awk '/bin/ {print $0 }' shell.txt sed 's/hp/samsung/' printers.txt how do i ensure that the correct input files I am working with are used? (5 Replies)
Discussion started by: Pauline mugisha
5 Replies

7. Shell Programming and Scripting

Read a file content with awk and sed

Hello , I have huge file with below content. I need to read the numeric values with in the paranthesis after = sign. Please help me with awk and sed script for it. 11.10.2009 04:02:47 Customer login not found: identifier=(0748502889) prefix=(TEL) serviceCode=(). 11.10.2009 04:03:12... (13 Replies)
Discussion started by: rmv
13 Replies

8. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies

9. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies

10. Shell Programming and Scripting

file name Manipulation using sed

Hi, I have a file name, for which I want to strip out the first bit and leave the rest... So I want to take the file name .lockfile-filename.10001 ,strip it and have only filename.10001 ... Thanking you all inadvance, Zak (6 Replies)
Discussion started by: Zak
6 Replies
Login or Register to Ask a Question