Moving line up if line starts with + sign.

Hello everyone,

I'm struggling with this command:

awk '!/^\+/{ORS=FS}/^\+/{ORS=RS}1' file1 > file2

What I want to do is to move any line that starts with the + sign 1 up, so its the continuation of the previous.

The above command is messing the whole output, can you please let me know what is wrong, or what else can be done?

Thanks a lot!

That will print the newline at the wrong end of the line, that is why it messes up. One way to achieve this might be:

awk 'NR>1 && !/^\+/{print RS}1 END{print RS}' ORS= file
1 Like

Great! It worked as I expected, thanks a lot!

Just curious, will below change help?

awk '!/^\+/{ORS=""}/^\+/{ORS=RS}1' file
awk 'NR>1 && !/^\+/{print RS}1 END{print RS}' ORS= file1

Should be (using gawk anyway, did not test with other awk versions):

awk 'NR>1 && !/^\+/{print RS}{print} END{print RS}' ORS= file1
$ cat test.sh
echo input file:
cat file1
echo
echo original approach did not work:
awk '!/^\+/{ORS=FS}/^\+/{ORS=RS}1' file1
echo
echo
echo scrutinizer approach throws a syntax error:
awk 'NR>1 && !/^\+/{print RS}1 END{print RS}' ORS= file1
echo
echo scrutinizer approach modified a little works:
awk 'NR>1 && !/^\+/{print RS}{print} END{print RS}' ORS= file1
echo
echo yoda suggested change did not help:
awk '!/^\+/{ORS=""}/^\+/{ORS=RS}1' file1
$ ./test.sh
input file:
Line 1
+ Line 2
Line 3
+ Line 4
+ Line 5
Line 6
+ Line 7
+ Line 8
+ Line 9
Line 10
Line 11

original approach did not work:
Line 1 + Line 2
Line 3 + Line 4
+ Line 5
Line 6 + Line 7
+ Line 8
+ Line 9
Line 10 Line 11

scrutinizer approach throws a syntax error:
awk: cmd. line:1: NR>1 && !/^\+/{print RS}1 END{print RS}
awk: cmd. line:1:                           ^ syntax error

scrutinizer approach modified a little works:
Line 1+ Line 2
Line 3+ Line 4+ Line 5
Line 6+ Line 7+ Line 8+ Line 9
Line 10
Line 11

yoda suggested change did not help:
Line 1+ Line 2
Line 3+ Line 4
+ Line 5
Line 6+ Line 7
+ Line 8
+ Line 9
Line 10Line 11
1 Like

Thanks for catching that. In BSD awk it happens to work without a semicolon, but there should be one behind the 1 :

awk 'NR>1 && !/^\+/{print RS}1; END{print RS}'  ORS= file

or indeed a {print} action instead of a 1 .

If the first line of input happens to start with a plus sign, all of the scripts provided so far will skip that line. If the input file could ever have this condition, the following alternative script should work:

awk '/^[+]/ {printf("%s", $0);next}
{printf("%s%s", NR > 1 ? "\n" : "", $0)}
END{print ""}' file1 > file2

With the sample input file hanson44 provided, the output placed into file2 will be:

Line 1+ Line 2
Line 3+ Line 4+ Line 5
Line 6+ Line 7+ Line 8+ Line 9
Line 10
Line 11

If the contents of file1 is:

+ Line 2
Line 3
+ Line 4
+ Line 5
Line 6
+ Line 7
+ Line 8
+ Line 9

the output placed into file2 will be:

+ Line 2
Line 3+ Line 4+ Line 5
Line 6+ Line 7+ Line 8+ Line 9

As always, if you're going to run this on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk .

@Don: I get the same output:

$ awk 'NR>1 && !/^\+/{print RS}1; END{print RS}' ORS= file
+ Line 2
Line 3+ Line 4+ Line 5
Line 6+ Line 7+ Line 8+ Line 9
1 Like

Ouch. Yes. Sorry for the misinformation, I had an error in my copy and paste that dropped the output.

Sorry,
Don