Remove EOL selectively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove EOL selectively
# 1  
Old 11-10-2011
Remove EOL selectively

Hi,

I have a text as below
Code:
test1 test2 test3\
test4 test5 test6 test7
newtest1 newtest2\
newtest3 newtest4 newtest5

And need this to be replaces to
Code:
test1 test2 test3 test4 test5 test6 test7
newtest1 newtest2 newtest3 newtest4 newtest5

So my requirement is to remove the EOL if the last character is \.

I tried my luck and searches but but unsuccessfull.. help here is very much appreciated.

Thanks
Praveen

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.
# 2  
Old 11-10-2011
Not sure what OS you're using or if the file contents will be static like the example you gave but this may work:

Code:
awk '{sub(/\\/, "")}!(NR%2){print $0" "p}{p=$0}' file.txt

Output:
test4 test5 test6 test7 test1 test2 test3
newtest3 newtest4 newtest5 newtest1 newtest2

Hope this helps point you in the right direction.
# 3  
Old 11-10-2011
Try something like this:
Code:
sed "/\\\\$/ { N; s/\\\\\n/ /}" filename

# 4  
Old 11-17-2011
Sorry , awk is not working as expected ....
Code:
#cat test_sudo
User_Alias      TEST=test1,test2,test3,test4,test5,\
                     test6,test7,ltest8,test9


#awk '{sub(/\\/, "")}!(NR%2){print $0" "p}{p=$0}' test_sudo
                     test6,test7,ltest8,test9 User_Alias        TEST=test1,test2,test3,test4,test5,

#sed "/\\\\$/ { N; s/\\\\\n/ /}" test_sudo
sed: 0602-404 Function /\\$/ { N; s/\\\n/ /} cannot be parsed.

This is an AIX machine

Last edited by Franklin52; 11-17-2011 at 04:04 AM.. Reason: Code tags
# 5  
Old 11-17-2011
Let a simple shell script do the job:
Code:
]$ cat test_sudo
User_Alias      TEST=test1,test2,test3,test4,test5,\
                     test6,test7,ltest8,test9
$ while read line
> do
> echo "$line"
> done
User_Alias      TEST=test1,test2,test3,test4,test5,                     test6,test7,ltest8,test9

The read shell-builtin has the capability to deal with \-NL by itself.
# 6  
Old 11-17-2011
Another approach:
Code:
awk '/\\$/{printf substr($0,1,length-1) FS; next}1' file


Use nawk or /usr/xpg4/bin/awk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selectively deleting newlines with sed

I have a file that look like this: >Muestra-1 agctgcgagctgcgaccc gggttatata ggaagagacacacacaccccc >Muestra-2 agctgcg agctgcgacccgggttatataggaagagac acacacaccccc >Muestra-3 agctgcgagctgcgaccc gggttatata ggaagagacacacacaccccc I use the following sed script to remove newlines from... (2 Replies)
Discussion started by: Xterra
2 Replies

2. Shell Programming and Scripting

How to remove spaces from a file selectively?

Hi i have a file in which i am doing some processing. The code is as follows: #!/bin/ksh grep DATA File1.txt >> File2.txt sed 's/DATA//' File2.txt | tr -d ‘ ‘ >> File4.xls As you can see my output is going in a xl file.The output consist of four columns/feilds out of which the first... (20 Replies)
Discussion started by: Sharma331
20 Replies

3. Shell Programming and Scripting

How to selectively NOT output some fileds?

Dear All I have a text file which has many columns (>10,000). I want to create a new text file which will NOT include following columns: 5,15,105,200. How can I do that in shell (or awk, perl)? Thanks. (6 Replies)
Discussion started by: littlewenwen
6 Replies

4. AIX

How to remove ^M from the EOL?

The only way I know of is manually as follows: To remove for example ^M from a file: - vi the file name that has ^M at the end of each line. - Hit <Esc> - Type :g/ - Hold the CNTRL key and press V and M then release the CNTRL key At the buttom you should see this by now: ... (3 Replies)
Discussion started by: mrn6430
3 Replies

5. Shell Programming and Scripting

How to selectively suppress perl output?

The following perl statement in a bash script consists of two substatements. I intend the first perl substatement (the assignment with glob) to get input from the preceding bash pipe, and the second perl substatement (the foreach loop) to output back to bash. However, the first perl substatement... (7 Replies)
Discussion started by: LessNux
7 Replies

6. Shell Programming and Scripting

Selectively Find/Replace in a file?

I have a file that is HTML encoded. Each line has something like this on each line.. <href=http://link.com/username.aspx>username </a> more info.. <a href=http://link.com/info1.aspx>info1</a> more code... <a href=http://link.com/info2.aspx>info2</a> I have one goal really.. to clean up the... (2 Replies)
Discussion started by: dragin33
2 Replies

7. UNIX for Advanced & Expert Users

Selectively Reformating a file using AWK

Dear users, I am new to AWK and have been battling with this one for close to a week now. Some of you did offer some help last week but I think I may not have explained myself very well. So I am trying again. I have a dataset that has the following format where the datasets repeat every... (5 Replies)
Discussion started by: sda_rr
5 Replies

8. Shell Programming and Scripting

how to selectively mount FS?

Hi all, I have this doubt as to whether we can selective mount FS .by taking the input from the vfstab? ie, suppose i want to mount tmpfs(actually i want to do it :))but i dont want to follow the conventional way through the script that actually does!! However i want to just collect that... (0 Replies)
Discussion started by: wrapster
0 Replies

9. Shell Programming and Scripting

Selectively splitting a file with C-shell?

I have a rather long csh script that works, but it's terribly ungraceful and takes a while from various loops. I only know enough code to get myself into trouble, so I'm looking for some guidance. I have a large file that is separated at intervals by the same line, like this: ... (2 Replies)
Discussion started by: fusi0n
2 Replies
Login or Register to Ask a Question