Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

awk problem

Original Message
Name: dhjackal
Date: November 13, 2006 at 08:10:57 Pacific
Subject: awk problem
OS: Linux
CPU/Ram: na
Model/Manufacturer: na
Comment:
I am reading a file and want to return $1 (a directory path) followed by the rest of the line i.e. $2 to the end of the line.

my script so far is

cat file.txt |while read line;
do

FILE=`echo $line | awk '{for (i=2; i<=NF; i++) printf("%s ", $i) printf("\n") }'`

done


But this is not working

All help apprectiaed

Declan



Report Offensive Message For Removal


Response Number 1
Name: Devaraj (by Fidy)
Date: November 13, 2006 at 20:58:30 Pacific
Subject: awk problem
Reply: (edit)
I didn’t quite understand your question. If you cud give me a sample input and ouput, may be I can help you..

Do you mean, you want to print only $1 and $2 for each line in the file?
If so-
Awk ‘{print $1,$2}’ filename > outfile


Regards,
Devaraj Takhellambam


Report Offensive Follow Up For Removal

Response Number 2
Name: dhjackal
Date: November 14, 2006 at 02:06:00 Pacific
Subject: awk problem
Reply: (edit)
Devaraj

Thanks for the reply, the problem is that I am reading a file which sometimes has two positional parameters but sometimes it has more. I.e..

File.txt

/Home/temp/user /Thisismyhome/
/Home/temp/newuser /Thisismyhome/
/Home/temp/bob /Thisismyhome and something/

When I print $1,$2 I get

/Home/temp/user /Thisismyhome/
/Home/temp/newuser /Thisismyhome/
/Home/temp/bob /Thisismyhome

But I need to include the “and something/” from the last line i.e. $3 and $4

So I want to print something like $1, $2* (the star being to the end of the line)

Any ideas?

Thanks again for you help

Declan


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: November 15, 2006 at 15:36:08 Pacific
Subject: awk problem
Reply: (edit)
You could create two awk scripts - the first gets just the first fields and the second gets from field 2 to the end of the line. I chose to create one awk script that places a pipe symbol between the first and second fields. Then I use a while loop to do the parsing. I export the variables if I want to use them outside of the inner while loop:

#!/bin/ksh

while read line;
do

t2fields=$(echo "$line" | awk '{
fx=$2
for (i=3; i<=NF; i++)
fx=fx" "$i
printf("%s|%s", $1, fx)
}')

echo "$t2fields" |while IFS="|" read f1 f2
do
export myf1="$f1"
export myf2="$f2"
done

echo "$myf1"
echo "$myf2"

done < file.txt
# end script

Maybe somebody smarter than I can come up with a better solution.

I hope I'm interpreting what you want correctly.


Report Offensive Follow Up For Removal

Response Number 4
Name: Devaraj (by Fidy)
Date: November 16, 2006 at 21:40:43 Pacific
Subject: awk problem
Reply: (edit)
as you said-
So I want to print something like $1, $2* (the star being to the end of the line)

End of the line..
You can try this although it is the same as printing the whole line....if you cud give me the sample input with different variations and what exactly you are trying to do..it will be easier...

awk '{ first=$1; min=index($0," ")-index($2,"/")+1;max=length($0);print "first one is", first,"second one is ",substr($0,min,max)}' infile

Devaraj Takhellambam

Regards,
Devaraj Takhellambam


Report Offensive Follow Up For Removal

Response Number 5
Name: Devaraj (by Fidy)
Date: November 17, 2006 at 02:14:37 Pacific
Subject: awk problem
Reply: (edit)
you may wan to do something like this...

awk '{ first=$1; min=index($0," ")-index($2,"/");max=leng th($0);print first,substr($0,min,max)}' infile


Regards,
Devaraj Takhellambam


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: awk problem 

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




My PC has been hijacked!

Lexmark 2600 Printer Issues

btk1w1 infected start here post

Unwanted message remians on screen

Slow boot time


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC