Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-26-2012, 10:54 AM
|
#1
|
Member
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114
Rep:
|
awk, print special character, how?
Hello,
Trying to figure out how to append the wildcard character * to each entry of the first column of a data file. I've tried various combinations of quotes and backslashes, but it just doesn't want to work for me.
Code:
awk '{print $1"*"}' somefile
Any input would be appreciated.
|
|
|
04-26-2012, 10:59 AM
|
#2
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,797
|
Quote:
Originally Posted by johnpaulodonnell
but it just doesn't want to work for me.
|
"Doesn't work" meaning you lose the rest of columns? Try
Code:
awk '{$1=$1"*"; print}' somefile
|
|
|
04-26-2012, 11:08 AM
|
#3
|
Member
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114
Original Poster
Rep:
|
Thanks for that. I just wanted to extract the first column but to tag * onto each entry from that column.
Code:
awk '{$1=$1"*"; print $1}' somefile
did the trick.
Appreciate your help.
|
|
|
04-26-2012, 11:31 AM
|
#4
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
How bizarre.... is that a MacOS thing? The original version of
yours works just fine here.
Code:
awk -F: '{print $1"*"}' /etc/passwd
root*
bin*
daemon*
adm*
lp*
sync*
shutdown*
halt*
mail*
news*
uucp*
operator*
games*
ftp*
smmsp*
mysql*
rpc*
sshd*
...
In what way was your original failing?
Cheers,
Tink
|
|
|
04-26-2012, 12:30 PM
|
#5
|
Member
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114
Original Poster
Rep:
|
I thought that was where the problem lay as I was getting a error upon executing this piece of code within a tcsh script (on a MAC yes)...
Turns out I had to add prior to the command and all is now well. I don't know what this does, but it works so I'm happy.
Code:
set list = `ls -l $dir/D*bp03 | awk '{print $9}' | awk 'BEGIN {FS="."} {print $2}' | awk 'BEGIN {ORS=" "} {print "D*"$0".Z"}'`
echo $list
set: No match.
Code:
set nonomatch
set list = `ls -l $dir/D*bp03 | awk '{print $9}' | awk 'BEGIN {FS="."} {print $2}' | awk 'BEGIN {ORS=" "} {print "D*"$0".Z"}'`
echo $list
D*NAMA.Z D*KGMA.Z D*TUND.Z D*PNDA.Z D*UVZA.Z D*KIG.Z D*MBAR.Z D*KMBO.Z
which is what I wanted.
|
|
|
04-26-2012, 12:35 PM
|
#6
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
"$1" prints only the first column, and he apparently wants all of the columns in the output.
Edit: just read the above more closely.
Let this be a lesson. Don't just say "it doesn't work" and leave everyone to guess what you mean. Explain exactly why it isn't working, and how it should work. Preferably with actual examples of input and output.
Anyway, if the number of columns is fixed and manageable, you could also simply include them in the print statement.
Code:
awk '{ print $1"*",$2,$3,$4,$5 }' somefile
Last edited by David the H.; 04-26-2012 at 12:36 PM.
|
|
|
04-26-2012, 12:48 PM
|
#7
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,797
|
Code:
set list = `ls -l $dir/D*bp03 | awk '{print $9}' | awk 'BEGIN {FS="."} {print $2}' | awk 'BEGIN {ORS=" "} {print "D*"$0".Z"}'`
Nice example of Don't parse ls and XyProblem. I'm not sure what that line is computing, but I'm sure there is a simpler way.
|
|
|
All times are GMT -5. The time now is 10:52 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|