Visit The New, Modern Unix Linux Community


Remove previous line if next & previous lines have same 4th character.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove previous line if next & previous lines have same 4th character.
# 1  
Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file.
Code:
bash-3.2$ cat abc_do_it.txt
grpg10so>show trunk group all status
grpg11so>show trunk group all status
grpg12so>show trunk group all status
GCPKNYAIGT73IMO         1440  1345       0     0     94     0     0 INSERVICE     93%    0% (    1 )
grpg13so>show trunk group all status
grpg14so>show trunk group all status
grpg15so>show trunk group all status
grpg16so>show trunk group all status
WHPLNYGRDS3S2B1B        336   312        0     1      0     0     0 INSERVICE     92%    6% (   23 )
grpg1so>show trunk group all status
grpg2so>show trunk group all status
NYCMNY54DS1S8Y1O        456   453        0     0      2     0     0 INSERVICE     99%    0% (    1 )
grpg3so>show trunk group all status
grpg4so>show trunk group all status
grpg5so>show trunk group all status
grpg6so>show trunk group all status
DRPKNYDP03TS2A1I        504   136        0     8      0     0     0 INSERVICE     26%   71% (  360 )
NYCQNYWSCM3SWW1O        336   323        0     0     12     0     0 INSERVICE     96%    0% (    1 )
grpg7so>show trunk group all status
grpg8so>show trunk group all status
grpg9so>show trunk group all status
mtng10so>show trunk group all status
NYCMNYBX47T1IM1I        1776  1752       0     0      0     0     0 INSERVICE     98%    1% (   24 )
WNYKNYBCCM1SWW1I        336   22         0     5      0     0     0 INSERVICE      6%   91% (  309 )
WNYKNYBCCM1SWW1O        336   131        0     0      0     0     0 INSERVICE     38%   61% (  205 )
WHPLNY02CA23IM1O        720   477        0     0     29     0     0 INSERVICE     66%   29% (  214 )
mtng11so>show trunk group all status
mtng1so>show trunk group all status
WNYKNYBCCM1SWW1I        96    0          0     0      0     0     0 INSERVICE      0%  100% (   96 )
NYCMNY1306TS2A2I        384   336        0     0      0     0     0 INSERVICE     87%   12% (   48 )
WHPLNY02CA0SIM1I        672   445        0    11      0     0     0 INSERVICE     66%   32% (  216 )
mtng2so>show trunk group all status
WNYKNYBCCM1SWW1I        240   0          0     0      0     0     0 INSERVICE      0%  100% (  240 )
WHPLNY02CA23IM1O        1080  1020       0     0     59     0     0 INSERVICE     94%    0% (    1 )
WNYKNYBCCM1SWW1O        144   2          0     0      7     0     0 INSERVICE      1%   93% (  135 )
mtng3so>show trunk group all status
WHPLNYWP08TS2A1I        192   191        0     0      0     0     0 INSERVICE     99%    0% (    1 )
WNYKNYBCCM1SWW1O        192   3          0     0      9     0     0 INSERVICE      1%   93% (  180 )
mtng4so>show trunk group all status
NYCMNY83CM2SWW1O        672   552        0     0    118     0     0 INSERVICE     82%    0% (    2 )
NYCMNYBSDS2S2B1B        48    0          0     0      0     0     0 INSERVICE      0%  100% (   48 )
NYCMNYBSDS0S2B1B        48    0          0     0      0     0     0 INSERVICE      0%  100% (   48 )
mtng5so>show trunk group all status
mtng6so>show trunk group all status
DRPKNYDP03TS2A1I        48    0          0     0      0     0     0 INSERVICE      0%  100% (   48 )
mtng7so>show trunk group all status
WHPLNY02CA0SIM1I        624   272        0    16      0     0     0 INSERVICE     43%   53% (  336 )
mtng8so>show trunk group all status
mtng9so>show trunk group all status
NYCMNY3723TS2A1I        240   56         0    27      0     0     0 INSERVICE     23%   65% (  157 )
nwtg1so>show trunk group all status
NWTPMOAB00T1PT1B        144   141        0     0      2     0     0 INSERVICE     97%    0% (    1 )
nwtg2so>show trunk group all status
nwtg3so>show trunk group all status
INDPMOXCGT00WW1O        1440  1343       0     0     49     0     0 INSERVICE     93%    3% (   48 )
nwtg4so>show trunk group all status
rpkg10so>show trunk group all status
rpkg11so>show trunk group all status
RCPKNJ02EADSVM1O        168   161        0     0      2     0     0 INSERVICE     95%    2% (    5 )
rpkg1so>show trunk group all status
rpkg2so>show trunk group all status
NWRKNJ0205TS2A1O        120   118        0     0      1     0     0 INSERVICE     98%    0% (    1 )
rpkg3so>show trunk group all status
rpkg4so>show trunk group all status
rpkg5so>show trunk group all status
rpkg6so>show trunk group all status
rpkg7so>show trunk group all status
rpkg8so>show trunk group all status

bash-3.2$


Last edited by Franklin52; 11-08-2012 at 07:32 AM.. Reason: Please use code tags for data and code samples
# 2  
try:
Code:
tac abc_do_it.txt | awk 'BEGIN{l=1}{if ($0 !~ />/ ) {print; l=0} else {if (l==0) {print;} ; l=1}}' | tac

# 3  
Thanx rdrtx1 , but problem is tac is not working in my system.

The above cat abc_do_it output is taken from one of one line command , I want to use |after that and then get the desired result.

tgstats `trunkgroups g | egrep "mtn|grp|rpk|nwt" | awk '{print $1}' | sort -u` | egrep -v Totals | egrep "^[mtn|grp|rpk|nwt]|\% \(" > abc_do_it

I want to use tgstats `trunkgroups g | egrep "mtn|grp|rpk|nwt" | awk '{print $1}' | sort -u` | egrep -v Totals | egrep "^[mtn|grp|rpk|nwt]|\% \(" | some_command_to_get_desired_output.


AWK logic is very good , its working but tac is not present in my system and I am not system admin.
# 4  
try: instead of tac use:
Code:
perl -e 'print reverse <>'

# 5  
Thanks !!!

It worked. Smilie

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #817
Difficulty: Medium
In CSS, E[foo="bar"] matches an E element whose "foo" attribute value is exactly equal to "bar".
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

3. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

4. Shell Programming and Scripting

Returning multiple outputs of a single line based on previous repeated lines

Hello, I am trying to return a time multiple times from a file that has varying output just before the time instance, i.e. cat jumped cat jumped cat jumped time = 1.1 cat jumped cat jumped time = 1.2 cat jumped cat jumped time = 1.3 In this case i would like to output a time.txt... (6 Replies)
Discussion started by: ryddner
6 Replies

5. 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

6. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "«"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 «890000000 «543209911 «CHNGOHG «000000001 «055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

7. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

8. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

9. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

10. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

Featured Tech Videos