- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Replace a word in a line
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
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
тАО12-27-2008 01:17 PM
тАО12-27-2008 01:17 PM
I have a problem in replacing a particular word in a file.
file format is like this:
1kkkkll
sjgkdsg
sggggggg
"ALL","sadf",,"55","Y",,"10","","7867"
"ALL","sadf",,"100","Y",,"100","","7868"
"ALL","sadf",,"100","N",,"100","","7868"
iewrowe
So on the 4th line,if the 5th word is "Y" then I want to change the 7th word.
I tried using perl,sed but problem it is changing the all the words equal to 100 or 10.
Thanks,
Priya
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-27-2008 01:41 PM
тАО12-27-2008 01:41 PM
Re: Replace a word in a line
One way:
# perl -nle '@F=split /,/;if ($F[4]=~/Y/) {print join ",",@F[0..5],qq("999"),@F[7..@F-1]} else {print}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-27-2008 03:13 PM
тАО12-27-2008 03:13 PM
Re: Replace a word in a line
Thanks for the prompt reply.
I tried with that its working:
But one problem when I'm trying to write into the same file is that its keeps the original line and duplicates the changed line as shown below:
perl -pi.bak -e '@F=split /,/; if ($F[4]=~/Y/){print join ",",@F[0..5],qq("999"),@F[7..@F-1]} else {print}' test
-- more test
1kkkkll
1kkkkll
sjgkdsg
sjgkdsg
sggggggg
sggggggg
"ALL","sadf",,"55","Y",,"999","","7867"
"ALL","sadf",,"55","Y",,"10","","7867"
"ALL","sadf",,"100","Y",,"999","","7868"
"ALL","sadf",,"100","Y",,"100","","7868"
"ALL","sadf",,"100","N",,"100","","7868"
"ALL","sadf",,"100","N",,"100","","7868"
iewrowe
iewrowe
Thanks,
Priya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-27-2008 03:36 PM
тАО12-27-2008 03:36 PM
Solution> But one problem when I'm trying to write into the same file is that its keeps the original line and duplicates the changed line as shown below...
...OK, so use this:
# perl -nli.bak -e '@F=split /,/;if ($F[4]=~/Y/) {print join ",",@F[0..5],qq("999"),@F[7..@F-1]} else {print}' file
...note the change of the '-p' for automatic printing of each record read to '-n' to suppress printing by default for each record read.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2008 02:01 AM
тАО12-28-2008 02:01 AM
Re: Replace a word in a line
It worked!!
Thanks :)
Priya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-28-2008 03:22 AM
тАО12-28-2008 03:22 AM
Re: Replace a word in a line
awk -F, '
BEGIN { OFS = ","; NEWSTR = "\"XYZ\"" }
(NF != 9) {
print $0
next
}
$5 == "\"Y\"" {
print $1, $2, $3, $4, $5, $6, NEWSTR, $8, $9
} ' file > file.new
sed would need a RE:
sed 's/\([^,]*,[^,]*,[^,]*,[^,]*,"Y",[^,]*,\)\([^,]*\)/\1"XYZ"/' file > file.new