1834275 Members
2407 Online
110066 Solutions
New Discussion

HELP

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

HELP

I'm having a heck of a time on something very simple. I have in input file that looks like
02/06/02 19:00:09
02/07/02 02:29:22
However I want to change it before I read it.
I want it all on the top line with a space in between like:
02/06/02 19:00:09 02/07/02 02:29:22
I need to do this in a script, not on command line. Any ideas greatly appreciated.

Thanks,
Bob


UNIX IS GOOD
10 REPLIES 10
Steve Steel
Honored Contributor

Re: HELP

Hi


Use $(cat $file)

Then you have 1 line

ex

echo $(cat $file)


steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
ian Dennison
Regular Advisor

Re: HELP

Only 2 lines per file, or multiples of 2 lines?

cat /tmp/filename |awk '{printf ("%s %s ", $1, $2)}'

will print each entry without putting a line feed in. Only will work for 2 lines total per file.

Share and Enjoy! Ian

Lets do it to them before they do it to us! www.fred.net.nz
harry d brown jr
Honored Contributor

Re: HELP

awk ' BEGIN { firsttime = 1 }
{
if ( firsttime == 1 ) {
firstline = $0
firsttime = 0
} else
{
print firstline, $0
firstline = ""
firsttime = 1
}
}
END {
if ( firsttime == 0 ) { print firstline }
}'


live free or die
harry
Live Free or Die
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: HELP

Hi Robert:

This will combine any number of lines:
cat myfile | while read X
do
echo "${X} \c"
done
echo
If it ain't broke, I can fix that.
Vincent Stedema
Esteemed Contributor

Re: HELP

Uhmmm....don't know if this will work:

cat file | awk 'BEGIN {ORS=" "} {print $0}' -

This will set the output record separator to a single space and then print each line in the file.

Regards,

Vincent
Nobody's Hero
Valued Contributor

Re: HELP

Thanks everyone, Wish I could think that fast.
After 6 years I sometimes still feel like a novice. Script is not my bag. Thanks Clay, I used yours, less typing. Thanks everyone, great ideas.

Bob
UNIX IS GOOD
Peter Kloetgen
Esteemed Contributor

Re: HELP

Hi Robert,

if you have to do this with a script, you could use the awk to manage it:
BEGIN { line = "" }
{
for ( NR <= 2 ) {
line = line " " $0 }
{
print line
}
}

the script can be called as follows:

awk -f script_name file_name

(if the file has more lines than two you have to change the script.... )

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
James R. Ferguson
Acclaimed Contributor

Re: HELP

Hi Robert:

If it were only the first two lines of the file that you wanted joined into one, then you could use this:

# awk '{if (NR<3) {X=$0;getline;X=X" "$0;print X} else {print $0}}' myfile

Regards!

...JRF...
Darrell Allen
Honored Contributor

Re: HELP

Hi Bob,

This will "join" pairs of lines no matter how many are in the file:

while read line1
do
read line2
echo $line1 $line2
done
Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Greg Stark_1
Frequent Advisor

Re: HELP

I think you can also do this:
cat $file |xargs echo

Greg