- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- replace certain chars in every line of a text file
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-09-2003 02:07 PM
06-09-2003 02:07 PM
I need to replace chars 7-10 in each line of a file with something else.. I looked and SED and TR but can't find the proper syntax.
Please help
thanx,
Steve
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2003 02:19 PM
06-09-2003 02:19 PM
Re: replace certain chars in every line of a text file
First create an awk script, my.awk.
{
if (length($0) >= 7)
{
s1 = substr($0,1,6)
s2 = "NEW!"
s3 = substr($0,11)
printf("%s%s%s\n",s1,s2,s3)
}
else printf("%s\n",$0)
}
Now:
awk -f my.awk oldfile > newfile.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2003 02:22 PM
06-09-2003 02:22 PM
Re: replace certain chars in every line of a text file
:= #Gives total lines in file (n)
:1,ns/str1/str2/g #replaces str1 with str2 in the entire file
HTH
Marty
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2003 02:24 PM
06-09-2003 02:24 PM
Re: replace certain chars in every line of a text file
:%s/old/new/g
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2003 02:33 PM
06-09-2003 02:33 PM
Re: replace certain chars in every line of a text file
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x001d63f96280d711abdc0090277a778c,00.html
I've saved it.
Maybe someday i will learn martian.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2003 05:14 PM
06-09-2003 05:14 PM
Re: replace certain chars in every line of a text file
#sed 's/Old/New/g' inputfile >outputfile
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 01:32 AM
06-10-2003 01:32 AM
Solutioncat file | sed 's/^\(.......\).../\1XXX/'
This means substitute, matching a pattern that starts at the beginning of the line ^, match any 7 characters and keep them, because they are enclosed in \( and \). Then match the next three characters. Replace this with whatever the first 7 characters were using \1 then add the new stuff you want, XXX. There is also no special processing required for lines shorter than 10 characters, as the pattern will simply fail to match.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 01:57 AM
06-10-2003 01:57 AM
Re: replace certain chars in every line of a text file
Enjoy, have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 02:31 AM
06-10-2003 02:31 AM
Re: replace certain chars in every line of a text file
cut -b 11- infile >f2
paste f1 f2 >outfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 03:00 AM
06-10-2003 03:00 AM
Re: replace certain chars in every line of a text file
You can achieve this as follows
for i in `cat
do
echo $i > (tmpfile1)
str=`cut -c 7-10 tmpfile1`
sed 's/'$str'/xxxx/' tmpfile1 >> /tmp/
done
where "xxxx" is your substitute string
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 08:06 AM
06-10-2003 08:06 AM
Re: replace certain chars in every line of a text file
didn't expect so many replies..
thanx a bunch
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 12:03 PM
06-10-2003 12:03 PM
Re: replace certain chars in every line of a text file
using sed 's/Old/New/g' wouldn't work in my case because I didn't have anything to match to.. the only constant in my case was that I needed to replace from char 7 to char 10.
I didn't try using suggested AWK scripts, because I'm not very familiar with awk and didn't know what it was doing ( and I don't want to run scripts that I don't know what they do )
anyhow.. I want to thank everybody for their suggestions.. its great to know that I can turn to these forums if I ever need any more help.
thanx,
Steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 05:35 AM
06-11-2003 05:35 AM
Re: replace certain chars in every line of a text file
now you should go back and assign points to these answers. It's another way to say thanks to those who've answered your questions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 01:06 PM
06-11-2003 01:06 PM
Re: replace certain chars in every line of a text file
#-----------------------
echo "1,\$s/\(......\)..../\1abcd/\n:wq!" | /usr/bin/ex xyz
#-----------------------
where "abcd" is the new text that you want to replace the old text, and "xyz" is the file to be modified.