- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- replace words with spaces
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
Discussions
Discussions
Forums
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
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
тАО07-13-2005 11:15 AM
тАО07-13-2005 11:15 AM
replace words with spaces
I have a file which contains lines with the following format:
S12|abc|123|456|111|999|
I need to replace the word or number which comes between 5th and 6th "|" (our case '999') with a space.
It would be great if it can be done for lines that start with "S".
Please help.
Thanks,
Pat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 03:38 PM
тАО07-13-2005 03:38 PM
Re: replace words with spaces
cat your_file | grep ^S | awk -F\| '{printf("%s|%s|%s|%s|%s| |\n",$1,$2,$3,$4,$5)}'
- Biswajit
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 03:49 PM
тАО07-13-2005 03:49 PM
Re: replace words with spaces
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 04:48 PM
тАО07-13-2005 04:48 PM
Re: replace words with spaces
echo "S12|abc|123|456|111|999|" | awk -F"|" '/^S/ { $6=" ";OFS="|";print $0 }'
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 04:58 PM
тАО07-13-2005 04:58 PM
Re: replace words with spaces
echo "S12|abc|123|456|111|999|" | perl -ne ' @arr=split(/\|/);$arr[5]=" ";$,="|";print @arr;'
or
perl -ne ' @arr=split(/\|/);$arr[5]=" ";$,="|";print @arr;'
hth.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 07:15 PM
тАО07-13-2005 07:15 PM
Re: replace words with spaces
First, let's use autosplit (-aFpattern)
lt09:/tmp 118 > cat xx.txt
S12|abc|123|456|111|999|ABRACA
lt09:/tmp 119 > perl -naF\\\| -e'$,="|";/^S/||next;$F[5]=" ";print@F' xx.txt
S12|abc|123|456|111| |ABRACA
lt09:/tmp 120 >
Note that if the last field is empty, which in our case is not true: it contains a newline, split without additional arguments will drop them. You will have to add
split /\|/, $_, -1
to retain the empty trailing fields. -a does not need that, because the fields are split *before* the chomp happens:
lt09:/tmp 126 > perl -naF\\\| -e'chomp;$,="|";/^S/||next;$F[5]=" ";print@F' yy.txt
S12|abc|123|456|111| |
lt09:/tmp 127 > perl -ne 'chomp;@arr=split(/\|/);$arr[5]=" ";$,="|";print @arr;' yy.txt
S12|abc|123|456|111| lt09:/tmp 128 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-13-2005 07:17 PM
тАО07-13-2005 07:17 PM
Re: replace words with spaces
lt09:/tmp 131 > cat xx.txt
S12|abc|123|456|111|999|ABRACA
T12|abc|123|456|111|989|ABRACA
lt09:/tmp 132 > perl -naF\\\| -e'$,="|";/^S/and$F[5]=" ";print@F' xx.txt
S12|abc|123|456|111| |ABRACA
T12|abc|123|456|111|989|ABRACA
lt09:/tmp 133 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2005 06:18 PM
тАО07-14-2005 06:18 PM
Re: replace words with spaces
awk -F"|" '/^S/ {$6=" ";OFS="|";print} file
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-15-2005 03:14 AM
тАО07-15-2005 03:14 AM
Re: replace words with spaces
Here is a short one-liner using awk. It will replace the string between the 5th and 6th columns if there is a leading "S" in the first column of your input file or else it will print the entire record.
# awk -F"|" '{if($1~/^S/){$6=" "; OFS="|"; print $0} else print $0}' input_file > output_file