LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 11: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, 11:59 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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, 12:08 PM   #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, 12:31 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 01: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, 01:35 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

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

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
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 03:16 AM
SLES cups do not print special character (spanish chars) Spatior SUSE / openSUSE 3 04-17-2012 12:13 PM
Scaping the ' character in awk print command DeepSeaNautilus Programming 4 09-17-2008 08:03 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 06:07 PM
i need help with a special character Hone101 Linux - Newbie 1 05-03-2003 08:21 PM


All times are GMT -5. The time now is 01:12 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration