1834437 Members
2288 Online
110067 Solutions
New Discussion

Scripting help

 
SAM_24
Frequent Advisor

Scripting help

Hi,

I want to combine two lines into one line

LABEL1:some output
LABEL2:some output
.
.

Want to print like
LABEL1:output LABEL2: output

Any help is appreciated.

Thanks.
Never quit
10 REPLIES 10
Rick Garland
Honored Contributor

Re: Scripting help

Check out the join command
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting help

Here's one approach:

#!/usr/bin/sh

INFILE="/mydir/myfile"
typeset -i10 I=0
cat ${INFILE} | while read X
do
if [[ $((${I} % 2)) -eq 0 ]]
then
echo "${X} \c"
else
echo "${X}"
fi
((I + 1))
done
if [[ $((${I} % 2)) -eq 1 ]]
then
echo
fi

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting help

Oops, I'm an idiot:

((I + 1))
should be:
((I += 1))
If it ain't broke, I can fix that.
Victor Fridyev
Honored Contributor

Re: Scripting help

You can try the following:
awk '{printf("%s", $0)
if( NR%2 == 0)print "" }' filename

HTH
Entities are not to be multiplied beyond necessity - RTFM
Jeff Carlin
Frequent Advisor

Re: Scripting help

Use the "\c" in the print line:

#print "LABEL1: ${output1} \c"
#print "LABEL2: ${output2}"

This will print:

LABEL1: some output LABEL2: some output

If this isn't what you mean, we could use a little more info on what you are trying to do.
Where wisdom is called for, force is of little use. --Of course, a hammer does wonders for relieving stress.
Hein van den Heuvel
Honored Contributor

Re: Scripting help

Like Jeff I am not entirely sure what you are requesting. Please provide an attachement with slightly more complete input data and desired output date.

If the data comes from 1 file, then join is not going to help much but maybe 'pr' can do it's trick.

Check out: ps -2 -t -s seperate.txt > combined.txt
The -s changes column based to seperator character based output (default tab).

Here is somehting in AWK actively looking for 'LABEL1:':

awk '/^[A-Z0-9]+:/{if (i++%2) {print one, $0} else {one=$0}}' seperate.txt > combined.txt


Here the part between // actually looks specifically for a line, beginning with (^) a series of once or more (+) characters in the range of A thru Z or 0 - 9 ([...]) followed by a colon (:).
If you just want any old line (like the pr solution) then simplify to:

awk '{if (i++%2) {print one, $0} else {one=$0}}' seperate.txt > combined.txt


If the 'labels' are strictly and only a series of Uppercase characters, followed by a series of numbers at the start of a line then the // becomes: /^[A-Z]+[0-9]+:/

Many more options possible, but you'll have to help us describe them if you can not figure it out yourself.

Hein.





Bharat Katkar
Honored Contributor

Re: Scripting help

Hi,
Is this what you are looking for!!

# more file1
LABEL1:some output
LABEL2:some output
#
# cat file1 | xargs
LABEL1:some output LABEL2:some output
#

Regards,
You need to know a lot to actually know how little you know
Muthukumar_5
Honored Contributor

Re: Scripting help

If you want to join two lines , then use as,

set -x
# Execute with a filename
i=0
while read line; do
echo -n "$line "
let i=i+1
if [[ $((i%2)) -eq 0 ]]; then
echo ""
fi
done < $1


-- muthu ---
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Scripting help

We can do it with sed and echo as,

set -x
# Execute with file - $1 filename

i=1
count=$(cat $1 | wc -l)

while [[ $i -le $count ]]; do

echo `sed -n $i,$(($i+1))p $1`

let i=i+2
done

-- muthu --
Easy to suggest when don't know about the problem!
Saravanan_11
Occasional Advisor

Re: Scripting help

If u want to combine every two lines into a single line then this will help.

awk '{line1=$0; getline; print line1 " " $0 }' filename

Else If u want to combine many lines into a single line then this will be useful

cat filename | xargs

-Saravanan