Operating System - HP-UX
1825766 Members
1968 Online
109687 Solutions
New Discussion

Awk/ GAWK Help Requested: How to get the space out of my lines

 
SOLVED
Go to solution
Hanif Kazemi
Occasional Advisor

Awk/ GAWK Help Requested: How to get the space out of my lines

Hi,

I have a text file, and from a specific line after, I would like to get the space out.

I mean I'd like to take the space between field1 and field2 ($1,$2) out and make it one field.

How can I merge them?
Example:
Field1 Field2
work/ hoop.htm

I would like to change the above line to:

work/hoop.htm


Thanks for your response.

4 REPLIES 4
Mel Burslan
Honored Contributor
Solution

Re: Awk/ GAWK Help Requested: How to get the space out of my lines

line="work/ hoop.htm"
field1=`echo $line| awk {'print $1'}
field2=`echo $line| awk {'print $2'}
line=`printf ${field1}${field2}`

if you are going to read from a fiel and want to do it for every line

cat file | while read line
do
field1=`echo $line| awk {'print $1'}
field2=`echo $line| awk {'print $2'}
line=`printf ${field1}${field2}`
echo $line >> newfile
done


hope this helps
________________________________
UNIX because I majored in cryptology...
Patrick Wallek
Honored Contributor

Re: Awk/ GAWK Help Requested: How to get the space out of my lines

You do the following:

awk '{print $1$2}'

Will smash the first and second fields together.
Patrick Wallek
Honored Contributor

Re: Awk/ GAWK Help Requested: How to get the space out of my lines

For all your requirements maybe something like:

awk '{if (NR >= 5) { print $1$2 } else { print $0}}' yourfile >resultfile

Where the 5 in the NR >= 5 is the line number you require. Do you need to print the rest of the line (everything after $1 and $2) on the lines you merge?
Sandman!
Honored Contributor

Re: Awk/ GAWK Help Requested: How to get the space out of my lines


# awk '{if($1~"work") print $1$2; else print $0}' infile > outfile

this approach preserves the entire input file while gluing only the matched line.

enjoy!