Moving line up if line starts with + sign.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving line up if line starts with + sign.
# 1  
Old 05-08-2013
Moving line up if line starts with + sign.

Hello everyone,

I'm struggling with this command:
Code:
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!
# 2  
Old 05-08-2013
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:
Code:
awk 'NR>1 && !/^\+/{print RS}1 END{print RS}' ORS= file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-08-2013
Quote:
Originally Posted by Scrutinizer
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:
Code:
awk 'NR>1 && !/^\+/{print RS}1 END{print RS}' ORS= file

Great! It worked as I expected, thanks a lot!
# 4  
Old 05-08-2013
Just curious, will below change help?
Code:
awk '!/^\+/{ORS=""}/^\+/{ORS=RS}1' file

# 5  
Old 05-09-2013
Code:
awk 'NR>1 && !/^\+/{print RS}1 END{print RS}' ORS= file1

Should be (using gawk anyway, did not test with other awk versions):
Code:
awk 'NR>1 && !/^\+/{print RS}{print} END{print RS}' ORS= file1

Code:
$ 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

Code:
$ ./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

This User Gave Thanks to hanson44 For This Post:
# 6  
Old 05-09-2013
Thanks for catching that. In BSD awk it happens to work without a semicolon, but there should be one behind the 1:
Code:
awk 'NR>1 && !/^\+/{print RS}1; END{print RS}'  ORS= file

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

Last edited by Scrutinizer; 05-09-2013 at 01:14 PM..
# 7  
Old 05-09-2013
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:
Code:
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:
Code:
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:
Code:
+ Line 2
Line 3
+ Line 4
+ Line 5
Line 6
+ Line 7
+ Line 8
+ Line 9

the output placed into file2 will be:
Code:
+ 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Homework & Coursework Questions

When I run the script, the cursor starts on the wrong line?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: It's a shell script using a looping logic, trap, tput, if, while. Most of the scripts in this book aren't written... (2 Replies)
Discussion started by: ckleinholz
2 Replies

3. Shell Programming and Scripting

sed - How to replace right part of equal sign (=) on a line

Hello. Using a bash script , I have a variable name for the file I want to modify FILE_TO_EDIT="/etc/my_config_file"And I have a variable name for the parameter to change PARAMETER="fallback_node" PARAMETER_NEW_VALUE="http://my_server_name.com/new_path" A config file may contain : 1°)... (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

5. Shell Programming and Scripting

Moving a line to the end of the file

I have a file with different directories in it. I would need to move one line within the file to the end of the list. Also not there could be blank line in the middle of it. Example /vol/fs1 /vol/fs2 /vol/fs3 /vol/fs4 /vol/fs5 /vol/fs6 /vol/fs7 So I would need /vol/fs2... (3 Replies)
Discussion started by: bombcan
3 Replies

6. Shell Programming and Scripting

Moving items to a new line

Hi, Let's I have the following strings (md5): 07177edf8261d28c6a003e583fcbe38c 0717c0037b3a20fc0f0998e673f228d5 0717d611a5d24374628b98e17fd00977,0717d611a5d24374628b98e17fd00977 07189a18afdae558bb5aadfe602e4a91 0719e97d481c239667f38a3e166bed74 071af3225fe50a1fdbb42c43aac313cc... (4 Replies)
Discussion started by: talfiq
4 Replies

7. Shell Programming and Scripting

How to strip '$' sign on the line

echo "dddd$dddd"|sed 's/$//' will return dddd echo "dddd$ddd"|tr -d '$' will return dddd I need to replace it with any other character or just live a blank space instead Thank you! (8 Replies)
Discussion started by: zam
8 Replies

8. HP-UX

Typing the @ sign creates new line.

Whenever I type the @ sign like for example when using a proxy ftp server, The system forces the cursor to jump to a new line. I know it has something to do with the terminal settings. How can I get this to stop and more importantly, how can I modify my profile to set this up whenever I login? ... (3 Replies)
Discussion started by: ricnetman
3 Replies

9. Shell Programming and Scripting

How to read a line when it starts with spaces

Hi , I use read command to get the input text, When i try to get the line starting with spaces or ending with spaces it automatically truncates the spaces and displays the remaining content. Code i tried (UserInput.sh): #!/bin/bash echo -n "Enter some text > " read text echo "You... (3 Replies)
Discussion started by: PrakashChinna
3 Replies

10. Shell Programming and Scripting

moving to a specified line number

Hi Is there any command or a trick that can take me to a specifed line number in unix . Suppose i have a file with 2000 lines and i would like to go to line number 1899 and then print out the contents of the line. regards Hrishy (2 Replies)
Discussion started by: xiamin
2 Replies
Login or Register to Ask a Question