I have two separate commands which I would like to join. Basically, I want to match a line and insert a character at the end of the previous line to the matched line
Here is what I have got
Any help is much appreciated thanks
Note that ripat's awk script will delete all empty lines and all lines that consist entirely of a string of "0" characters.
The following ed script doesn't ignore empty lines and doesn't ignore lines that only contain a string of "0"s. Note that the patterns accepted by awk (which uses extended regular expressions) and ed (which uses basic regular expressions) are slightly different. If that difference doesn't matter to you, you can try this simple ed script:
With the file f containing a few more lines than were in ripat's example:
where the next to the last line just contains a space character, the ed script above produces the output:
while ripat's awk script produces the output:
If your pattern or the string to be added to lines before those matching the pattern contain any slash (/) characters, replace the slashes in the ed g and s commands with a character (other than a newline character) that will never appear in your pattern or added text. I frequently use a control-G for this when processing files that don't contain any ASCII BEL (sometimes called alert) characters (it shows up nicely when I'm editing the script with vi, but it is sometimes hard to describe in text to naive users).
If the difference between BREs and EREs matters, the following awk script will produce the same output as the ed script above. With any of these scripts, if the regular expression contains any meta-characters used in the type of RE you're using, they will have to be escaped appropriately. If you're using ed, you also have to consider what can happen with ampersand (&) and backslash digit (\x 1 <= x <= 9) in the replacement string. With both ed and awk, special attention will also be required if the pattern and replacement strings contain any dollar signs ($).
As always when writing new awk scripts, if you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of /bin/awk or /usr/bin/awk.
Note that although I used the Korn shell to test these scripts, any POSIX conforming shell will work. If you don't need the script to provide default values for the file to be processed, the pattern to use, and the string to be added to the lines prior to the lines containing the pattern, you can easily hard wire those values into either of the above scripts and use any shell you want.
Hope this helps...
This User Gave Thanks to Don Cragun For This Post:
Hello,
Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text:
123, foo1, bar1, short text1, dat1e, stable_pattern
124, foo2, bar2, long text
with few
lines, date,... (4 Replies)
Use and complete the template provided. If you don't, your post may be deleted!
1. The problem statement, all variables and given/known data:
I have to make as home work several commands with gerp find and sed
2. Relevant commands, code, scripts, algorithms:
FIND command
-use command find... (8 Replies)
Hi guys,
anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line?
The end of each line will be replaced with a special char "#".
I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines... (12 Replies)
Hello,
My apologies if this has been posted elsewhere, I have had a look at several threads but I am still confused how to use these functions. I have two files, each with 5 columns:
File A: (tab-delimited)
PDB CHAIN Start End Fragment
1avq A 171 176 awyfan
1avq A 172 177 wyfany
1c7k A 2 7... (3 Replies)
Hi All,
I have 2 file. ACC_NUM contains only account numbers & ACC_DETAIL contains all information including account number seperated by ~ delimiter.
i am serching the account number in ACC_DETAIL from ACC_NUM. If it is exist, then copy all information in ACC_DETAIL_NEW file. For that i... (7 Replies)