I would like only print out the line that start change from "+" to "-" or "-" to "+".
Situation in Input file 2, is a bit more challenge.
I have no much idea about how to solve it out
Thanks for any advice.
It looks like the script you were trying would just print the 1st line in each file for each different value in the 1st field. The following awk program seems to do what you want:
awk '
$1 == f1 || NR == 1 {
f0 = $0 "\n"
f1 = $1
next
}
{ print f0 $0
f0 = ""
f1 = $1
}' input_file
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 .
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:
If you don't have /usr/xpg4/bin/awk , I assume you are not using a Solaris system running the SunOS operating system.
This is a fairly simple awk script. Here it is again with comments explaining what each line does:
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".
From the comments in my code in message #12 in this thread, can you figure out which line needs to change so an input line will be printed twice when the 1st field changes on three consecutive lines? If you can figure out which line needs to change, can you figure out how to change it?
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.)
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.)
to:
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
# newline character.)
it should do what you want.
If you re-read this thread, you might notice that the script ahamed101 provided in message #5 in this thread is a condensed version of this script. My comment in message $6 in this thread pointed out that that script did what you are now requesting instead of doing what you originally requested.