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 |
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-04-2009, 05:10 PM
|
#1
|
|
Member
Registered: Jul 2009
Location: Slovakia
Posts: 132
Rep:
|
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
|
|
|
|
11-04-2009, 05:20 PM
|
#2
|
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,006
Rep: 
|
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
|
|
|
|
11-04-2009, 05:24 PM
|
#3
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
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)
|
|
|
|
11-04-2009, 06:00 PM
|
#4
|
|
Member
Registered: Jul 2009
Location: Slovakia
Posts: 132
Original Poster
Rep:
|
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
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.
|
|
|
|
11-04-2009, 06:05 PM
|
#5
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
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..
|
|
|
|
11-05-2009, 08:17 AM
|
#6
|
|
Member
Registered: Jul 2009
Location: Slovakia
Posts: 132
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:17 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
|
|