Help with print out line that have different record in specific column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with print out line that have different record in specific column
# 8  
Old 02-24-2014
Hi Don Cragun,

I try to run your code :
Code:
awk ' $1 == f1 || NR == 1 { f0 = $0 "\n" f1 = $1 next } { print f0 $0 f0 = "" f1 = $1 }' Input_file

awk:  $1 == f1 || NR == 1 { f0 = $0 "\n" f1 = $1 next } { print f0 $0 f0 = "" f1 = $1 }
awk:                                        ^ syntax error
awk:  $1 == f1 || NR == 1 { f0 = $0 "\n" f1 = $1 next } { print f0 $0 f0 = "" f1 = $1 }
awk:                                                                     ^ syntax error

Is it because I don't have /usr/xpg4/bin/awk install ?
I seems like run using /usr/bin/awk

Thanks for any advice.
# 9  
Old 02-24-2014
Syntax error is due to missing semicolon. Add semicolon to separate multiple statements within a line
Code:
awk ' $1 == f1 || NR == 1 { f0 = $0 "\n" ; f1 = $1 ; next } { print f0 $0 ; f0 = "" ; f1 = $1 }' Input_file

This User Gave Thanks to anbu23 For This Post:
# 10  
Old 02-24-2014
Quote:
Originally Posted by perl_beginner
Hi Don Cragun,

I try to run your code :
Code:
awk ' $1 == f1 || NR == 1 { f0 = $0 "\n" f1 = $1 next } { print f0 $0 f0 = "" f1 = $1 }' Input_file

awk:  $1 == f1 || NR == 1 { f0 = $0 "\n" f1 = $1 next } { print f0 $0 f0 = "" f1 = $1 }
awk:                                        ^ syntax error
awk:  $1 == f1 || NR == 1 { f0 = $0 "\n" f1 = $1 next } { print f0 $0 f0 = "" f1 = $1 }
awk:                                                                     ^ syntax error

Is it because I don't have /usr/xpg4/bin/awk install ?
I seems like run using /usr/bin/awk

Thanks for any advice.
Do you see any difference between my code:
Code:
awk '
$1 == f1 || NR == 1 {
        f0 = $0 "\n"
        f1 = $1
        next
}
{       print f0 $0
        f0 = ""
        f1 = $1
}' input_file

and what you have above? Please try running my code before saying my code doesn't work. Removing newlines from the middle of an awk program might not change anything and might completely change the meaning. Most of the newlines you removed changed the behavior of my suggested code. You can turn my awk code into a 1-line script as suggested by anbu23; I, however, prefer readable code where I can see the structure of the code by the indentation.

What operating system are you using? I said:
Quote:
If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk.
If you don't have /usr/xpg4/bin/awk, I assume you are not using a Solaris system running the SunOS operating system.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 02-24-2014
Hi anbu23,

Thanks for point out my mistakes of Don Cragun's code.

---------- Post updated at 08:14 AM ---------- Previous update was at 07:57 AM ----------

Hi Don Cragun,

Sorry for misunderstanding and misuse your awk program.
I'm using x86_64 GNU/Linux system.

I'm currently trying to understand the logic of your awk program.
Really thanks for your advice and knowledge sharing.

Apologize for my mistake.
Thanks again Smilie
# 12  
Old 02-24-2014
This is a fairly simple awk script. Here it is again with comments explaining what each line does:
Code:
awk ' 
$1 == f1 || NR == 1 {   # If the 1st field is the same as the 1st field on the
                        # previous line, or if this is the 1st line in the file:
        f0 = $0 "\n"    #       Save the current line with a newline for output
                        #       when we see a different value in the 1st field.
                        #       ($0 is the current line without the terminating
                        #       newlien character.)
        f1 = $1         #       Save the current 1st field to compare to the 1st
                        #       field on the next line.
        next            #       Skip to next input line.
}
{                       # To get to here, the 1st field on this line is not the
                        # same as the 1st field on the previous line:
        print f0 $0     #       Print the saved line and the current line.  The
                        #       print command adds a newline at the end of
                        #       whatever it prints.
        f0 = ""         #       Clear the saved line.  (We do not want to print
                        #       the current line twice if the 1st field in the
                        #       next line does not match the 1st field in this
                        #       line.)
        f1 = $1         #       Save the current 1st field to compare to the 1st
                        #       field on the next line.
}' input_file           # The input for this script is a file named "input_file".

These 2 Users Gave Thanks to Don Cragun For This Post:
# 13  
Old 02-24-2014
Dear Don Cragun,

Really thanks and appreciate your input.
Thanks for spending your time to guide me and explain your awk program in detail.

Super thanks you.

---------- Post updated at 10:42 PM ---------- Previous update was at 09:54 PM ----------

Dear Don Cragun,

After run your awk program, I got one similiar question need your advice :
Input file 1:
Code:
+ 123897 125349 
- 125727 125836 
- 127179 128103 
+ 128356 128848 
- 128476 130282 
- 135728 136490
+ 136845 138219 
- 138318 139845

Output file 1:
Code:
+ 123897 125349 
- 125727 125836 
- 127179 128103 
+ 128356 128848 
+ 128356 128848 
- 128476 130282 
- 135728 136490
+ 136845 138219 
+ 136845 138219 
- 138318 139845

Input file 2:
Code:
- 127179 128103 
+ 128356 128848 
- 128476 130282 

Output file 2:
Code:
- 127179 128103 
+ 128356 128848 
+ 128356 128848 
- 128476 130282 

Input file 3:
Code:
+ 127179 128103 
- 128356 128848 
+ 128476 130282 
- 135728 136490

Output file 3:
Code:
+ 127179 128103 
- 128356 128848 
- 128356 128848 
+ 128476 130282 
+ 128476 130282 
- 135728 136490

Basically I hope that able to duplicate the record if it shown as "- + -" or "+ - +" becomes "- + + -" or "+ - - +".

Kindly let me know if you not too sure about what I ask.
Thanks again.
# 14  
Old 02-24-2014
Yes, a bit confused. Sample input/output?

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Overwrite specific column in xml file with the specific column from adjacent line

I have an xml file dumped from rrd file, that I want to "patch" so the xml file doesn't contain any blank hole in the resulting graph of the rrd file. Here is the file. <!-- 2015-10-12 14:00:00 WIB / 1444633200 --> <row><v> 4.0419731265e+07 </v><v> 4.5045912770e+06... (2 Replies)
Discussion started by: rk4k
2 Replies

2. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. Shell Programming and Scripting

Problem facing to compare different column and print out record with smallest number

Hi, Input file 1 : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Desired Output file 1 : 37170 37196 77 51 Input file 2 : 37174 37195 73 0 37170 37196 77 0 Desired Output file 2 : 37174 37195 73 0 (1 Reply)
Discussion started by: cpp_beginner
1 Replies

5. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

6. Shell Programming and Scripting

Execution problem with print out record that follow specific pattern

Hi, Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"? Input file : a comp92 2404242 2405172 b comp92 2405303 2406323 b comp92 2408786 2410278 a comp92 2410271 2410337 a comp87 1239833 1240418 b comp87... (3 Replies)
Discussion started by: patrick87
3 Replies

7. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

8. Shell Programming and Scripting

Print first and last line from multiline record

Hi - I'm new to working with multiline records and I'm going nuts trying to do something that seems simple. Input: Tue May 1 14:00 Header Record 1 is valid. Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15 Tue May 1 14:03 Header Record 1 is valid. Tue May 1 14:03 processing data... (4 Replies)
Discussion started by: Catullus
4 Replies

9. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question