LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-04-2009, 05:10 PM   #1
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Rep: Reputation: 16
print FS (field separator) in awk


Hi gurus is possible to print string that matches the FS in awk ?

Input:
Code:
bbbb454ccc999vvv
aaaa444ggg777bbb
mmmm789jjj456kkk
Desired Output:
Code:
bbbb454vvv
aaaa444bbb
mmmm789kkk
Maybe this can be done another way but I am interesting how to print FS.

I tried that:
Code:
awk 'BEGIN {FS="\([0-9]*\)"} {print $1 "\1" $3}' input
bbbbvvv
aaaabbb
mmmmkkk
 
Old 11-04-2009, 05:20 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
So what you want to do is cut out those middle 6 chars?
Code:
#!/bin/bash
while read -r LINE ; do
   echo "${LINE:0:7}${LINE:13}"
done
 
Old 11-04-2009, 05:24 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
what do you mean match the FS. FS is the field separator. by default its white space including tabs. From your output, you just want to get rid of 7th to 12th position , in that case use substr(). Please start reading the gawk documentation (my sig)
 
Old 11-04-2009, 06:00 PM   #4
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Original Poster
Rep: Reputation: 16
I know what is FS for, but is also special variable like $1 so when I can print $1 why cannot I print FS ? I thing problem Is that I have to acces FS throught array because FS has more values (another for each column) but i dont know how??? maybe from some temporary variables I dont know.

Quote:
Originally Posted by ghostdog74 View Post
From your output, you just want to get rid of 7th to 12th position
Yes this example looks like this but I just simplified It cause of better understanding.

Lets say I have file /etcg/roups which contains DIFFERENT count of users

Code:
group1:*:23:jani,feri,jozi,pali,name1,name2,name3...
users2:*:32:user,user2,user3...
And also I have file which contains logins which should be deleted from groups.

1. I am writing script which use awk with delimiter ":[^:]*:[^:]*:"
2. so in $1 i will have name of group and in $2 the comma separated users.
3. then I will use some gsub() or something to $2 string for deleting users
4. and finaly I will need to "compose" variables to previous format (print $1 "FS" "$2")

Thank you

PS perl and other utils cannot be used just pure AWK or SED or some GNU utils

Last edited by wakatana; 11-04-2009 at 06:01 PM.
 
Old 11-04-2009, 06:05 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
It doesn't work that way since you are giving regular expression to FS. how do you expect print to know which FS you want ?

do it another way, since you are using /etc/groups and its a structured file format, just use ":" as delimiter and get field1 and field4.
Code:
# awk -F":" '{print $1,$4}' file
group1 jani,feri,jozi,pali,name1,name2,name3...
users2 user,user2,user3..
 
Old 11-05-2009, 08:17 AM   #6
wakatana
Member
 
Registered: Jul 2009
Location: Slovakia
Posts: 133

Original Poster
Rep: Reputation: 16
Thanks this should work, Also print FS wolud be nice if AWK supports it

I want replace all "mar" string with ","

Still have one little bug
Code:
$cat testovaci
users:*:20:marek,marecek,mar,martin,mar,jomar,martinka,martinko,mar,jomar,mar,mar
$awk -F':' '{gsub("(^mar,)|(,mar$)|(,mar,)",",",$4); print $1":"$2":"$3":"$4}' testovaci
users:*:20:marek,marecek,martin,jomar,martinka,martinko,jomar,mar
Why I still have ",mar" at the end of output ?



Thanks a lot

Last edited by wakatana; 11-05-2009 at 08:21 AM.
 
  


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
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM
How to parameterize which field awk should print? dbland07666 Linux - Newbie 2 10-29-2007 03:49 PM
perl input field separator Tinkster Programming 5 10-18-2004 04:08 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
My field separator changes when using awk Helene Programming 3 05-01-2004 08:10 AM


All times are GMT -5. The time now is 07:29 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration