Visit The New, Modern Unix Linux Community


Help with concatenate multiple line into one line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with concatenate multiple line into one line
# 1  
Help with concatenate multiple line into one line

Hi,

Do anybody experience how to concatenate multiple line into one line by using awk or perl command?

Input file:
Code:
>set1
QAWEQRQ@EWQEASED
ASDAEQW
QAWEQRQTQ
ASRFQWRGWQ

From the above Input file, it got 5 lines

Desired output file:
Code:
>set1
QAWEQRQ@EWQEASEDASDAEQWQAWEQRQTQASRFQWRGWQ

I hope to concatenate all the line exclude ">" into a line.
It means at the desired output file, it only can contain 2 line. First line is a line with ">" and another line is concatenate multiple line into one long single line.

Thanks for any advice.
# 2  
An awk:
Code:
awk '{ORS=/^>/?"\n":"";print}' infile

If you have a multiple set file (and adding last rc as @RudiC proposes):
Code:
awk '{ORS=sub(/^>/,"\n>")?"\n":"";print}END{print "\n"}' infile


Last edited by Klashxx; 05-12-2014 at 12:52 PM..
# 3  
To make that a correct *nix text file by adding a new line char at the end, try:
Code:
awk '{ORS=/^>/?"\n":"";print} END{printf "\n"}' file

# 4  
Try deleting the new line with tr:-
Code:
tr -d "\n" < infile > outfile

It works for me, but of course there is no new-line at the end, so:-
Code:
RBATTE1> cat outfile
EWQEASEDASDAEQWQAWEQRQTQASRFQWRGWQRBATTE1>



Robin
# 5  
Another approach:
Code:
awk '
        />/ {
                $0 = ( NR == 1 ? $0 : RS $0 RS )
                print
        }
        !/>/ {
                ORS = ""
                print
        }
        END {
                print "\n"
        }
' file

# 6  
Nice & clean @Yoda , just one point i .. i would use next to avoid the second matching this way:
Code:
awk '
        />/ {
                $0 = ( NR == 1 ? $0 : RS $0 RS )
                print
                next
        }
        {
                ORS = ""
                print
        }
        END {
                print "\n"
        }
' infile

This User Gave Thanks to Klashxx For This Post:
# 7  
Another one:
Code:
awk '{$1=RS $1 ORS}NR>1' OFS= RS=\> file

(As long as there are no extra ">" in the ">" headers..)


--
otherwise
Code:
awk '/^>/{if(NR>1)print RS; $1=$1 RS}1 END{print RS}' ORS= file


Last edited by Scrutinizer; 05-12-2014 at 03:19 PM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #815
Difficulty: Medium
In CSS, E[foo] matches an E element with a 'foo' class name.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

3. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. 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

6. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

7. Shell Programming and Scripting

awk concatenate every line of a file in a single line

I have several hundreds of tiny files which need to be concatenated into one single line and all those in a single file. Some files have several blank lines. Tried to use this script but failed on it. awk 'END { print r } r && !/^/ { print FILENAME, r; r = "" }{ r = r ? r $0 : $0 }' *.txt... (8 Replies)
Discussion started by: sdf
8 Replies

8. Shell Programming and Scripting

KSH: Reading a file line by line into multiple arrays

Hi - I have a file that contains data in this format:- #comment value1 value2 value3 #comment value4 value5 value6 value7 #comment value8 value9 I need to read value1, value2 and value3 into one array, value4 value5 value6 and value7 into another array and value8 and value9 into a 3rd... (2 Replies)
Discussion started by: sniper57
2 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

Concatenate strings line by line

Hi, I have a noob question . Can someone help me how to concatenate line by line using this variables? var1: Apple| Banana| var2: Red Yellow then how can I concatenate both line by line? in which the result would be: Apple|Red Banana|Yellow just to generate a row result i was... (6 Replies)
Discussion started by: hagdanan
6 Replies

Featured Tech Videos