- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: find and replace for strings in shell scripts
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
06-28-2004 10:00 PM
06-28-2004 10:00 PM
Now I have to replace the string after the 3th Pipe, with another string.
How do I do the same in unix shell script?
Suposse the file has contents as below.[ 2 lines]
B100|ABCD|1234|AM20|TEST
B100|ABCDAM20|1234|AM20|TEST
Now I want to replace the AM20 with TPID after the 3rd Pipe, and not in any other place
if I use sed command like this..
echo $LINE | sed -e 's/AM20/TPID/' >>CPQFILE.tmp
it will also replace ABCDAM20 in the second line as ABCDTPID which I DONT want to happen..
PS: I am reading the file line by line and outputting in a temp file.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2004 10:08 PM
06-28-2004 10:08 PM
Re: find and replace for strings in shell scripts
We can use the check pattern as |AM20|. It will be replaced with |TPID|
echo
It will do globally on the full file.
Regards,
Muthukumar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2004 10:12 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2004 10:12 PM
06-28-2004 10:12 PM
Re: find and replace for strings in shell scripts
We can do the above with tr command too.
Use the command setup as like,
echo $LINE | tr -c '|AM20|' '|TPID|'
It will work too.
Regards,
Muthukumar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2004 10:13 PM
06-28-2004 10:13 PM
Re: find and replace for strings in shell scripts
with vi:
%s/\(^.*|.*|.*|\)AM20\(|.*\)/\1TPID\2
with ed:
,s/\(^.*|.*|.*|\)AM20\(|.*\)/\1XXX\2
good luck,
Thierry.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2004 12:12 PM
06-29-2004 12:12 PM
Re: find and replace for strings in shell scripts
what about using awk?
awk -F\| '{$4="your_string";print}' your_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2004 02:16 PM
06-29-2004 02:16 PM
Re: find and replace for strings in shell scripts
sed -e 's/^\([^|]*|[^|]*|[^|]*|\)\([^|]*\)\(.*\)$/\1NEWSTUFF\3/' < oldfile > newfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2004 02:23 PM
06-29-2004 02:23 PM
Re: find and replace for strings in shell scripts
awk -F\| '{OFS="|";$4="your_string";print}' your_file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 03:28 AM
06-30-2004 03:28 AM
Re: find and replace for strings in shell scripts
cut -f 1-3 -d"|" test > test1
cut -f4 -d"|" test |sed -e"s/AM20/xxx/"> test2
cut -f "5-" -d"|" test > test3
paste -d "|" test1 test2 test3 > test
Rory
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2004 03:53 AM
06-30-2004 03:53 AM
Re: find and replace for strings in shell scripts
$>cat file
B100|ABCD|1234|AM20|TEST
B100|ABCDAM20|1234|AM20|TEST
$>cat run
#!/opt/perl/bin/perl
while (
($one,$two,$three,$four,$five)=split /\|/,$_;
$four=~ s/^AM20$/TPID/g;
print join '|', $one,$two,$three,$four,$five;
}
$>./run < file
B100|ABCD|1234|TPID|TEST
B100|ABCDAM20|1234|TPID|TEST
regards,
Fred
"Reality is just a point of view." (P. K. D.)