Visit Our UNIX and Linux User Community


Help on Script of line delete and add


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on Script of line delete and add
# 1  
Old 02-24-2012
Bug Help on Script of line delete and add

Hey guys ,

I want create one script which give below output form my input:

Input file A
Code:
ABC12 122
ABC12 -0.7[dB]
ABC12 126
ABC12 0.0[dB]
ABC12 1210
ABC12 -0.7[dB]
ABC12 128
ABC12 17.3[dB]
ABC12 120
ABC12 21.9[dB]
ABC12 124
ABC12 19.5[dB]
ABC23 131
ABC23 130
ABC23 122
ABC23 120
ABC23 22.9[dB]

Output File B
Code:
ABC12 122  -0.7[dB]
ABC12 126   0.0[dB]
ABC12 1210 -0.7[dB]
ABC12 128  17.3[dB]
ABC12 120  21.9[dB]
ABC12 124  19.5[dB]
ABC23 131  N/I
ABC23 130  N/I
ABC23 122  N/I
ABC23 120  22.9[dB]

Thanks In Adavnce

Last edited by Franklin52; 02-26-2012 at 05:19 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-25-2012
awk

Hi,
Try this one,

Code:
Two Versions of solution.
 
1. awk 'BEGIN{col2=0;}{c=0;if( $0 ~ /dB/){print $1,col2,$2;c=1;}if(NR != 1 && c == 0 && p !~ /dB/ ){print p,"N/I";}col2=$2;p=$0;}' File
 
2. awk '{a[NR]=$0;if($0 ~ /dB/){b[NR]=$2}} END{for(i=1;i<NR;i++){ if( a[i] !~ /dB/){ if(b[i+1]~ /^$/){b[i+1]="N/I"}; print a[i],b[i+1]}}}' File

Cheers,
Ranga:-)

Last edited by rangarasan; 02-25-2012 at 03:40 AM.. Reason: added additional solutions
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 02-25-2012
Code:
sed -n -e '/ABC12/{h;n;H;x;/dB/{s/\n/ /;s/ABC[0-9][0-9]//2;p}};/ABC23/{/131/!{h;n;H;x;/dB/!p;/dB/{s/ABC23//2;s/\n/ /;p}};/131/p}' a  |  \
awk '{print NF<3?$0"  N/I":$0}'

This User Gave Thanks to complex.invoke For This Post:
# 4  
Old 02-25-2012
Code:
awk 'p && /dB/{print p FS $2;p=x;next} p{print p FS "N/I"} {p=$0} END{if(p)print p FS "N/I"}' infile

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-26-2012
Thanks a lot guys .....


Scrutinizer Its great solution ...

Previous Thread | Next Thread
Test Your Knowledge in Computers #704
Difficulty: Medium
Under the same-origin policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Mismatched free() / delete / delete [] line no missing

Could you tell me the possibilities of the reason to get the Mismatched free() / delete / delete . I unable to see the line no in the valgrind report. it displays the function name. with that function name, I am not able to find where exactly the issue is there.I am getting the Mismatched free()... (3 Replies)
Discussion started by: SA_Palani
3 Replies

2. UNIX for Dummies Questions & Answers

Script to delete files line by line in .txt document

Friends, I am in need to delete files from a directory on a regular basis. I want to place all the files to be deleted in delete .txt file and require a script to delete files, line by line from this delete.txt file. Please help me with this script as am new to unix scripting. (3 Replies)
Discussion started by: fop4658
3 Replies

3. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Shell Programming and Scripting

Delete line with match and previous line quoting/escaping problem

Hi folks, I've list of LDAP records in this format: cat cmmac.export.tmp2 dn: deviceId=0a92746a54tbmd34b05758900131136a506,ou=devices,ou=customer,ou=nl,o=upc cmmac: 00:13:11:36:a5:06 dn: deviceId=0a92746a62pbms4662299650015961cfa23,ou=devices,ou=customer,ou=nl,o=upc cmmac:... (4 Replies)
Discussion started by: tomas.polak
4 Replies

6. UNIX for Dummies Questions & Answers

Bash script to delete file input on command line

1) I wrote a script and gave the desired permissions using "chmod 755 scriptname". Now if i edit the script file, why do i need to set the permission again? Didn't i set the permission attribute.. or if i edit the file, does the inode number of file changes? 2) I am running my unix on a server... (1 Reply)
Discussion started by: animesharma
1 Replies

7. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

8. Shell Programming and Scripting

script to delete character in a line

Hi, Please help me to edit a file, with the following changes, 1) serach for XYZ , appears at the start of line, delete a character present in specified column number in a line. 2) This needs to be repeated in multiple rows and not the entire file Thanks (1 Reply)
Discussion started by: abc_81
1 Replies

9. Shell Programming and Scripting

Help with search and delete/add/modify script

Gurus, I need to run a script on multiple XML files in different directories and do the following (the output can be redirected to create a new file) 1. Search a pattern like "abc.mno.xyz" in an XML file, once detected, the script should delete one line above and 3 lines below (including the... (2 Replies)
Discussion started by: inditopgun
2 Replies

10. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

Featured Tech Videos