How to print empty line when the a field is changed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print empty line when the a field is changed
# 1  
Old 06-14-2008
How to print empty line when the a field is changed

Hi all,

I have this input file ..

BSS01 107 Swafieh 11/06/2008 12:06:57
BSS01 111 Ramada_Hotel 12/06/2008 11:37:20
BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS08 802 Jada_SDH 12/06/2008 09:53:59
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 811 Sarfa 12/06/2008 09:53:57
BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00

I need to print empty line when the second field is changed.
like this

BSS01 107 Swafieh 11/06/2008 12:06:57

BSS01 111 Ramada_Hotel 12/06/2008 11:37:20

BSS01 147 Kalha_Rep 11/06/2008 19:13:39
BSS01 147 Kalha_Rep 11/06/2008 19:13:39

BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37
BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS08 802 Jada_SDH 12/06/2008 09:53:59

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58
BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 811 Sarfa 12/06/2008 09:53:57

BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00
BSS08 821 Qaser_SDH 12/06/2008 09:54:00

I tried this awk but still doesnt work ..
any help is appreciated ..

BEGIN { site = $2 }

/BSS/{ if ( $2 == site )

printf "%s\n",$0; site = $2

else

printf "\n" ; site = $2
}

Thanks
# 2  
Old 06-14-2008
Code:
awk 'NR==1{v=$2}$2!=v{v=$2;print ""}1' file

Regards
# 3  
Old 06-14-2008
I want to help you, but I don't quite understand the question. Can you re-phrase it?
# 4  
Old 06-14-2008
Thanks for the quick response.

I tried your code, the ouput was like

BSS01 107 Swafieh 11/06/2008 12:06:57

BSS01 111 Ramada_Hotel 12/06/2008 11:37:20

BSS01 147 Kalha_Rep 11/06/2008 19:13:39

BSS01 147 Kalha_Rep 11/06/2008 19:13:39

BSS06 601 Abu_Alanda_Caragat 12/06/2008 02:57:11

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS06 601 Abu_Alanda_Caragat 12/06/2008 11:10:37

BSS08 802 Jada_SDH 12/06/2008 09:53:59

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 805 Rakeen_Batter 12/06/2008 09:53:58

BSS08 811 Sarfa 12/06/2008 09:53:57

BSS08 821 Qaser_SDH 12/06/2008 09:54:00

BSS08 821 Qaser_SDH 12/06/2008 09:54:00

BSS08 821 Qaser_SDH 12/06/2008 09:54:00


any suggestions ??????
# 5  
Old 06-14-2008
Dear na5m

I mean when the second field is changed I want to print new line.
To group the lines that have the same second field together.

regards
# 6  
Old 06-14-2008
Please Mr. Franklin Could you explain your code for me ...
Thanks very very much for your efforts. Smilie
# 7  
Old 06-14-2008
For what it's worth, here's a partial solution:
Code:
$ echo file | sort --key=2,2

I'll post a full solution, if I can make it that far Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

3. Programming

[SOLVED] C++ print next line until line.empty

Hi, could you please help with the following: I have an input file like: one two three four five six I want to print the lines starting from 'three' to the empty line. Something like that: if ( line == "three" ) { while ( !line.empty() ) { cout <<... (0 Replies)
Discussion started by: apenkov
0 Replies

4. Shell Programming and Scripting

print non empty column/field value

I have below file 25-09-2012 24-09-2012 19-09-2012 31-07-2012 30-04-2012 30-03-2012 ASIAEXFVNV N/A CEU 4 DMIRSOA N/A CAS 2 2 2 DMIRSOA N/A MIDMT 2 NFIAL22 N/A HVNY 11 11 11 NFIAL22 N/A NYAL3 11 11 11 NFIAL22 N/A NYCN 11 11 11 ... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

5. Shell Programming and Scripting

print - for printing whole line, but delimeters are changed to spaces

Hi consider the source file R|field1|field2 using the print statement in awk prints it as R field1 field2 Is there an easier way to print the whole line in its original format (i.e. with | delimiters) than doing print $1"|"$2"|"$3 ?? Thanks Storms (1 Reply)
Discussion started by: Storms
1 Replies

6. Shell Programming and Scripting

awk print last line returns empty string

hello I have a file with lines of info separated with "|" I want to amend the second field of the last line, using AWK my problem is with geting awk to return the last line this is what I am using awk 'END{ print $0 }' myFile but I get an empty result I tried the... (13 Replies)
Discussion started by: TasosARISFC
13 Replies

7. Shell Programming and Scripting

awk - if field is empty, move line to new file

I have a script with this statement: /usr/xpg4/bin/awk -F"" 'NR==FNR{s=$2;next}{printf "%s\"%s\"\n", $0, s}' LOOKUP.TXT finallistnew.txt >test.txt I want to include logic or an additional step that says if there is no data in field 3, move the whole line out of test.txt into an additional... (9 Replies)
Discussion started by: scriptr2be
9 Replies

8. UNIX for Dummies Questions & Answers

Help required on printing the line if third field is not a empty space

Hi Experts, I want to print the lines whose third field in non-empty/blank space. i.e. INPUT FILE/B] dcdccghjrry0yd cont dcdccttrk820529 cont rdekedfsSCr dcdccttrdky2d0y cont rdekedfsSC2 ... (3 Replies)
Discussion started by: krao
3 Replies

9. Shell Programming and Scripting

how to print field n of line m

Hi everyone, I have a basic csh/awk question. How do I print a given field from a given line in a given file? Thanks in advance! (11 Replies)
Discussion started by: Deanne
11 Replies

10. Shell Programming and Scripting

sed - print old line and new changed line

hello, i have a file "TEST" and want to change the digit(s) after "=" . but i also want to print the old entry with a comment (for information). i want to use "sed", is it possible ? file: TEST = 10 # comment default value: 0 sed , with "p" , i can print the old entry, but i want to... (2 Replies)
Discussion started by: bora99
2 Replies
Login or Register to Ask a Question