- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: inserting delimiters
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2008 07:55 PM
10-22-2008 07:55 PM
I need advise on how to format strings in a specific way.
I have this data extracted from the passwd file
user1,YNNNNNNNNNNNNNNNNNNN
user2,YNNNNNNYYNNNNNNNNNNN
field1 = username
field2 = user flags in the descriptions field
from the above format I wanted to create this output:
user1,Y,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
user2,Y,N,N,N,N,N,N,Y,Y,N,N,N,N,N,N,N,N,N,N
TIA!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2008 09:43 PM
10-22-2008 09:43 PM
Solution-lpe = loop trhough input and print $_ with newline
First split on non-Word (The , and the new-line) saving username in $u, flags in $f.
The re-join with commas, that username, and the flags where those are re-split on 'nothing'.
Enjoy,
Hein.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2008 09:43 PM
10-22-2008 09:43 PM
Re: inserting delimiters
================================================
sed -e 's/\(.*,\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\1\2,\3,\4,\5,\6,\7,\8,\9,/' \
-e 's/\(.*,.,.,.,.,.,.,.,.,\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\1\2,\3,\4,\5,\6,\7,\8,\9,/' \
-e 's/\(.*,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,\)\(.\)\(.\)\(.\)\(.\)/\1\2,\3,\4,\5/'
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 01:00 AM
10-23-2008 01:00 AM
Re: inserting delimiters
Disadvantage is it calls multiple sed statements. But it's easier to understand if you're just starting out.
cat testdata
user1,YNNNNNNNNNNNNNNNNNNN
user2,YNNNNNNYYNNNNNNNNNNN
cat testdata | tr "," " " | sed -e 's/Y/Y /g' | sed -e 's/N/N /g' | sed -e 's/ /,/g' | sed -e 's/$,//g'
Output
user1,Y,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N
user2,Y,N,N,N,N,N,N,Y,Y,N,N,N,N,N,N,N,N,N,N,N
Approach
Decided a space will be the final Field delimeter.
So replaced that first comma with a space
tr "," " "
Then put a space beside all Y's and N's
sed -e 's/Y/Y /g' | sed -e 's/N/N /g'
Then replaced all spaces with a comma
sed -e 's/ /,/g'
And then finally removing the comma that would be left at the end of the string from the previous step
sed -e 's/,$//g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 02:20 PM
10-23-2008 02:20 PM
Re: inserting delimiters
These options are superb!
Thanks again!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 02:29 PM
10-23-2008 02:29 PM
Re: inserting delimiters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 02:32 PM
10-23-2008 02:32 PM
Re: inserting delimiters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 02:48 PM
10-23-2008 02:48 PM
Re: inserting delimiters
> statements.
It doesn't need to. Multiple "-e" options
should do the job with one "sed".
Of these two:
> [...] 's/$,//g'
> [...] 's/,$//g'
the second one works better. And is "tr"
really needed?
bash$ echo 'user1,YNNNNNNNNN' | \
sed -e 's/,/ /' -e 's/Y/Y /g' -e 's/N/N /g' -e 's/ /,/g' -e 's/,$//g'
user1,Y,N,N,N,N,N,N,N,N,N
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 04:13 PM
10-23-2008 04:13 PM
Re: inserting delimiters
I can explain my regular expressions:
1) It captures everything up to the first comma and puts it back.
2) It then captures 8 characters and puts each back followed by a ",".
3) Since \# only works for 1..9, it does 8 more, after first accepting the first 8.
4) Then it does the final 4 after accepting the first 16.
- Tags:
- regex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 07:43 PM
10-23-2008 07:43 PM
Re: inserting delimiters
Ha! As if that could stop me...
bash$ echo 'user1,YNNNNNNNNN' | \
sed -e 's/,/ /' -e 's/\([NY]\)/\1 /g' -e 's/ /,/g' -e 's/,$//g'
user1,Y,N,N,N,N,N,N,N,N,N
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 07:43 PM
10-23-2008 07:43 PM
Re: inserting delimiters
$ perl -lpe'($u,$f)=split /\W/; $_=join q(,),$u,split(//,$f)'
Really is...
while ($line =
($user, $flags) = split (/\W/, $line);
@letters = split(//, $flags);
$new_line = join ',' , $user, @letters;
print $new_line, "\n";
}
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 07:57 PM
10-23-2008 07:57 PM
Re: inserting delimiters
Thanks for the notes Hein! Will likely to play and consider it in the code too
Cheers!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 08:07 PM
10-23-2008 08:07 PM
Re: inserting delimiters
>echo 'user1,YNNNNNNNNN' | \
sed -e 's/,/ /' -e 's/\([NY]\)/\1 /g' -e 's/ /,/g' -e 's/,$//g'
It is obvious this isn't going to work if the user name is like "YESNO".
I'm also not sure why you want to change to spaces then back?
Note: the "g" in 's/,$//g' is useless.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 09:48 PM
10-23-2008 09:48 PM
Re: inserting delimiters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2008 10:24 PM
10-23-2008 10:24 PM
Re: inserting delimiters
awk -F, '
{
printf $1 ","
for (i=1; i < 20; ++i)
printf substr($2,i,1) ","
print substr($2,20,1)
} '
- Tags:
- awk