1833811 Members
3761 Online
110063 Solutions
New Discussion

Re: formate text file

 
SOLVED
Go to solution
Karthik S S
Honored Contributor

formate text file

Hi,

I have a text file something like this,

--------------------

host1
host1-status
host2
host2-status
host3
host4-status

...
.. runs in to several 100 similar lines ...

---------------------

Now I want to format this file to like the following,
-------
host1 host1-status
host2 host2-status
host3 host3-status
------

can I do this using sed or someother command??

Thanks
karthik

For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
14 REPLIES 14
Massimo Bianchi
Honored Contributor
Solution

Re: formate text file

Hi,
from "man paste"

Combine pairs of lines into lines

paste -s -d"\t\n" file



HTH,
Massimo
Jean-Louis Phelix
Honored Contributor

Re: formate text file

hi,

A simple shell solution :

#!/usr/bin/sh
while read A
do
read B
echo $A $B
done < file

Regards
It works for me (© Bill McNAMARA ...)
Hai Nguyen_1
Honored Contributor

Re: formate text file

Karthik,

Try this script:

#!/bin/sh

grep 'status$' your_input_file > /tmp/file2
grep -v -f /tmp/file2 your_input_file > /tmp/file1
paste file1 file2 > /tmp/result.txt


Hai
Hai Nguyen_1
Honored Contributor

Re: formate text file

Karthik,

Please change the last line to:

paste /tmp/file1 /tmp/file2 > /tmp/result.txt


Hai
Vicente Sanchez_3
Respected Contributor

Re: formate text file

Hello,

This cuold help:

cont=1
while read i
do
if [ $cont = 1 ];then
a1=$i
let cont=$cont+1
continue
fi
a2=$i
echo "$a1 $a2">>prueba1
cont=1
done
HTH, Vicente.
Jean-Louis Phelix
Honored Contributor

Re: formate text file

Hi,

And with sed :

sed '{
N
s/\n/ /
}' file

Regards.
It works for me (© Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: formate text file

I had forgotten awk ...

awk '{ printf "%s ",$0 ; getline ; print}' file

Regards.
It works for me (© Bill McNAMARA ...)
Karthik S S
Honored Contributor

Re: formate text file

Hi all,

Thanks a lot for your responses. I ashamed that I never came across the paste command being a Unix Admin so long ... :-(

But I am still in trouble,

My actual file looks like this,

-----------------------------------------------
t3wg-01:/:<2>id read u1pcu1
Battery Life Used : 7 days, 15 hours
t3wg-01:/:<3>id read u1pcu2
Battery Life Used : 7 days, 15 hours
t3wg-02:/:<2>id read u1pcu1
Battery Life Used : 156 days, 22 hours
t3wg-02:/:<3>id read u1pcu2
Battery Life Used : 475 days, 22 hours
t3wg-03:/:<2>id read u1pcu1
Battery Life Used : 654 days, 3 hours
t3wg-03:/:<3>id read u1pcu2
Battery Life Used : 93 days, 5 hours
-----------------------------------------------


Now when I do

paste -s -d"\t\n" textfile

the o/p comes like this,
-----------------------------------------------
t3Battery Life Used : 7 days, 15 hours
t3Battery Life Used : 7 days, 15 hours
t3Battery Life Used : 156 days, 22 hours
t3Battery Life Used : 475 days, 22 hours
t3Battery Life Used : 654 days, 3 hours
t3Battery Life Used : 93 days, 5 hours
t3Battery Life Used : 654 days, 3 hours
t3Battery Life Used : 93 days, 5 hours
test2-t3Battery Life Used : 93 days, 5 hours
----------------------------------------------

that is the first line of every pair is not pasted/displayed fully .. what could be the problem??

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: formate text file

Hi Jean,

Your awk command only prints the second line :-(

Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Massimo Bianchi
Honored Contributor

Re: formate text file

Hi,
i think hte problem can be in the <> sign.
I will have same tests and then tell you what i find.
Massimo
Massimo Bianchi
Honored Contributor

Re: formate text file

try this:

cat test.input | paste -s -d"\t\n" -

where test.input is your file.

It worked for me :)

Massimo

Jean-Louis Phelix
Honored Contributor

Re: formate text file

Hi Karthik,

Strange ... I tested it before writing !

awk '{ printf "%s ",$0 ; getline ; print}' file

printf "%s ",$0 print current line without LF
getline gets next line
print prints the whole second line including LF

It still works for me ... Like the paste command in fact ... So I'm wondering if your file couldn't contain control characters ?

Regards.
It works for me (© Bill McNAMARA ...)
James R. Ferguson
Acclaimed Contributor

Re: formate text file

Hi:

# paste -s -d"\t\n" filename

...works fine for me. Do:

# cat -etc filename

...beforehand to see if you have any special characters embedded (?).

Regards!

...JRF...
Massimo Bianchi
Honored Contributor

Re: formate text file

Hi, I tested also the other solution, and it work.

I did a cut&paste of your output, so i think there may be some hidden control character.

Try with

cat -tve yourfile

and let's see what is there !!


Massimo