- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Automating changes in text files
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
тАО09-05-2007 07:31 PM
тАО09-05-2007 07:31 PM
I am new to unix,partcularly to shell scripting.I have an appplication which generates data files which I have to edit (using vi) and search for a line starting with a particular pattern (numeric or alphanumeric eg: "003" or "abc" 0r "a13" etc....).If i find that particular line I have check whether the next line starts with another particular pattern and if it is NOT I need to combine both the lines in to one (ie, no CR or "enter") in between.I tried with sed and some scripting but still no progress. I will be extremely happy if I get some script in which I can pass on the file name and the patterns to be searched and have the file changed (as I have a lot ).Please help .Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2007 09:33 PM
тАО09-05-2007 09:33 PM
Re: Automating changes in text files
awk '
/^003/ || /^abc/ || /^a13/ {
printf "%s", $0
getline
if ($0 ~ "^next" )
print "" # newline
}
{ print } ' file
If you want the patterns to be variables:
awk -v p1="^003" -v p2="^abc" -v p3="^a13" -v p4="^next" '
$0 ~ p1 || $0 ~ p2 || $0 ~ p3 {
printf "%s", $0
getline
if ($0 ~ p4)
print "" # newline
}
{ print } ' file
And since you are using EREs, you can combine the N patterns:
awk -v p1="^(003)|(abc)|(a13)" -v p4="^next" '
$0 ~ p1 {
printf "%s", $0
getline
if ($0 ~ p4)
print "" # newline
}
{ print } ' file
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 12:22 AM
тАО09-06-2007 12:22 AM
Re: Automating changes in text files
Thanks for that. But please clarify the following (may be too silly..)
1) Can I just type in all this to file and run that as a script ?
2) will the same work on Linux also ??
3) Can u suggest changes so that I can use it as a script and I can run it just by typing
./script
eg: ./scr myfile 000 003
and all the lines in myfile starting with 000 will combine with the next line if it is NOT starting with 003.(other lines no changes)
Thanks again,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 01:02 AM
тАО09-06-2007 01:02 AM
Re: Automating changes in text files
try running the attached as you have suggested; it may or may not work.
regards,
John K.
- Tags:
- missing attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 03:01 AM
тАО09-06-2007 03:01 AM
Re: Automating changes in text files
Here is a perl one-liner that works for the problem as stated
$ perl -e '$a=shift; $b=shift; while (<>) {if (/^$a/){chomp;print;$_=<>;$_="\n".$_ if /^$b/} print}
'
It user "^" to anchor the match pattern to the beginning of lines as specified.
Questions to ask yourself.
- What to do if there are mutliple starts
- Glue together just one next line or everything up to the next end
- What to do if there is no end in sight
- What to do if there extra ends
- Can the start line ever be a last line in a file.
Here is the perl script in slow motion:
$ perl -e ' ' # start perl with scrip in text
$a=shift; # pick up start pattern from command line
$b=shift; # pick up stop
while (<>) { # loop through input file
if (/^$a/) { # looks like the trigger?
{chomp; # prepare by removing new-line
print; # pritn first chunk already
$_=<>; # read next chunk
$_="\n".$_ # pre-pend a new-line only...
if /^$b/ # if end pattern seen
}
print # print the current line
} # loop
Hope this helps some,
Hein van den Heuvel (at gmail dot com)
HvdH Performance Consulting
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 07:04 AM
тАО09-06-2007 07:04 AM
Re: Automating changes in text files
# sed '/^000/N;/.*\n003/s/\n/ /p' file
~cheers
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 08:17 AM
тАО09-06-2007 08:17 AM
Re: Automating changes in text files
# ./uxscript file 000 003
...contents of uxscript are listed below.
======================= uxscript =======================
#!/usr/bin/sh
sed "/^$2/N;/.*\n$3/!s/\n/ /p" $1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 09:08 AM
тАО09-06-2007 09:08 AM
Re: Automating changes in text files
Yes.
>2) will the same work on Linux also??
It should.
>3) Can you suggest changes so that I can use it as a script and I can run it just by typing
./script
eg: ./scr myfile 000 003
Note the current script just creates a new file, if you want to update it, you would need to rename it.
Ah, it is much easier than what I thought you needed.
#!/bin/sh
# Usage: $0 file pat1 pat2
awk -v p1="^$2" -v p2="^$3" '
$0 ~ p1 {
printf "%s", $0
getline
if ($0 ~ p2)
print "" # newline
}
{ print } ' $1
>Sandman: To execute sed construct from within a shell script do as shown:
Yes, I thought about using sed like that. I thought I had to look for N patterns at the same time.
Your script inserts a blank, mine doesn't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 05:27 PM
тАО09-06-2007 05:27 PM
Re: Automating changes in text files
Handly, I tried ur script in Linux (#!/bin/sh)(not yet on HP)
./denn newfile orgfile 000 003.
If I am not wrong the newfile should contain what I need.But it is still 0Kb.Please advise. Thank you
John and Hein,I will try your suggestions and get back. Thanks a lot....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-06-2007 06:25 PM
тАО09-06-2007 06:25 PM