2

I have a file in linux called 1.txt with multiple lines and need to merge every three lines

into one line.I have mentioned the output below and need to know how to do that

[xxx@c0040229 ~]$ cat 1.txt
c0043726.xxx.xx
50G,3.6G,44G,8%,/opt/tibco    
200K,/opt/tibco/scripts
c0043725.xxx.xx
50G,9.5G,38G,21%,/opt/tibco
196K,/opt/tibco/scripts
c0043724.xxx.xx
50G,1.6G,46G,4%,/opt/tibco
92K,/opt/tibco/scripts

Out put should be

c0043726.xxx.xx,50G,3.6G,44G,8%,/opt/tibco,200K,/opt/tibco/scripts
c0043725.xxx.xx,50G,9.5G,38G,21%,/opt/tibco,196K,/opt/tibco/scripts
c0043724.xxx.xx,50G,1.6G,46G,4%,/opt/tibco,92K,/opt/tibco/scripts

Can somebody help me ?

2
  • First ask a question. The only question in what you've typed is "can somebody help me". Post the input and your expected output. Then mention what you've tried so that people can offer constructive advice. Commented Aug 1, 2014 at 6:51
  • I have edited the question with input as in "cat 1.txt" and out put as c0043726.xxx.xx,50G,3.6G,44G,8%,/opt/tibco,200K,/opt/tibco/scripts c0043725.xxx.xx,50G,9.5G,38G,21%,/opt/tibco,196K,/opt/tibco/scripts c0043724.xxx.xx,50G,1.6G,46G,4%,/opt/tibco,92K,/opt/tibco/scripts Commented Aug 1, 2014 at 6:57

4 Answers 4

3

I think you can achieve what you want with the paste command:

cat 1.txt | paste -d',' - - -
Sign up to request clarification or add additional context in comments.

4 Comments

[xxx@c0040229 ~]$ cat 1.txt | paste -d',' - - - ,200K,/opt/tibco/scriptsbco ,196K,/opt/tibco/scriptsibco ,92K,/opt/tibco/scriptsibco
it is different output
Your whole command should be just: cat 1.txt | paste -d',' - - -
It looks like your file might have Windows' line endings. In that case, could you try this? cat 1.txt | sed 's/\r//g' | paste -d',' - - -
1

Use the paste command and give it stdin (-) as input three times like this:

$ cat 1.txt 
c0043726.xxx.xx
50G,3.6G,44G,8%,/opt/tibco
200K,/opt/tibco/scripts
c0043725.xxx.xx
50G,9.5G,38G,21%,/opt/tibco
196K,/opt/tibco/scripts
c0043724.xxx.xx
50G,1.6G,46G,4%,/opt/tibco
92K,/opt/tibco/scripts
$ paste -d, - - - < 1.txt 
c0043726.xxx.xx,50G,3.6G,44G,8%,/opt/tibco,200K,/opt/tibco/scripts
c0043725.xxx.xx,50G,9.5G,38G,21%,/opt/tibco,196K,/opt/tibco/scripts
c0043724.xxx.xx,50G,1.6G,46G,4%,/opt/tibco,92K,/opt/tibco/scripts

Paste is normally used to 'paste -- merge corresponding or subsequent lines of files', and if you feed it with stdin three times, you get the desired effet.

4 Comments

for me output is always below . where is the problem ? [xxx@c0040229 ~]$ paste -d, - - - < 1.txt ,200K,/opt/tibco/scriptsbco ,196K,/opt/tibco/scriptsibco ,92K,/opt/tibco/scriptsibco
is the file being transferred from a Windows computer to a UNIX computer or vice versa?
what does cat -e 1.txt output?
Thanks For all you help .There is strange character ^M comming in 1.txt , which preventing me to slove this issue. [xxx@c0040229 ~]$ cat -v 1.txt | xargs -L 1| sed -e 's/ /,/g' c0043726.itcs.hp.com^M 50G,3.6G,44G,8%,/opt/tibco^M 200K,/opt/tibco/scripts^M c0043725.itcs.hp.com^M 50G,9.5G,38G,21%,/opt/tibco^M 196K,/opt/tibco/scripts^M c0043724.itcs.hp.com^M 50G,1.6G,46G,4%,/opt/tibco^M 92K,/opt/tibco/scripts^M Now it is ok after removing after ^M with sed command .This creature only visible by cat -v command. Thanks Again for your help
0

A simple script can also accomplish what you want. Reading your datafile and then merging 3 lines into one. Below the output is just echoed to the screen for illustration, but you can simply redirect each echo statement to a file using the >> append operator. This uses a case statement and simple counter. You can save a line or two using $cnt % 3 modulo operator to test for the 3rd line. Here is a short example:

#!/bin/bash

declare -i cnt=0

while IFS=$'\n' read -a line || test -n "$line"; do 
    ((cnt++))
    case "$cnt" in
      [1-2] ) echo -n "$line,";;
      3     ) echo "$line"; cnt=0;;
    esac
done < "$1"

exit 0

output:

$ ./3lines.sh dat/3line.dat
c0043726.xxx.xx,50G,3.6G,44G,8%,/opt/tibco,200K,/opt/tibco/scripts
c0043725.xxx.xx,50G,9.5G,38G,21%,/opt/tibco,196K,/opt/tibco/scripts
c0043724.xxx.xx,50G,1.6G,46G,4%,/opt/tibco,92K,/opt/tibco/scripts

Comments

0

You can use awk 'NR % 3 !=0 {printf $0;printf " "} NR % 3 ==0 {print " "}' fileName.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.