LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-26-2012, 10:54 AM   #1
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Rep: Reputation: 15
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.
 
Old 04-26-2012, 10:59 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,797

Rep: Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087
Quote:
Originally Posted by johnpaulodonnell View Post
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
 
Old 04-26-2012, 11:08 AM   #3
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
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.
 
Old 04-26-2012, 11:31 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 04-26-2012, 12:30 PM   #5
johnpaulodonnell
Member
 
Registered: Jun 2006
Location: Galway
Distribution: Suse 10.1
Posts: 114

Original Poster
Rep: Reputation: 15
I thought that was where the problem lay as I was getting a
Code:
set: No match
error upon executing this piece of code within a tcsh script (on a MAC yes)...

Turns out I had to add
Code:
set nonomatch
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.
 
Old 04-26-2012, 12:35 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"$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.
 
Old 04-26-2012, 12:48 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,797

Rep: Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087Reputation: 2087
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 02:16 AM
SLES cups do not print special character (spanish chars) Spatior SUSE / openSUSE 3 04-17-2012 11:13 AM
Scaping the ' character in awk print command DeepSeaNautilus Programming 4 09-17-2008 07:03 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
i need help with a special character Hone101 Linux - Newbie 1 05-03-2003 07:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration