Insert text into line if that line doesn't contain another string using sed

sed insert multiple lines at line number
sed insert line at line number
sed insert file at line number
awk insert line at line number
sed insert line after first match
sed insert line before match
sed insert line after last match
sed insert line at end of file

I am merging a number of text files on a linux server but the lines in some differ slightly and I need to unify them.

For example some files will have line like

id='1244' group='american' name='fred',american

Other files will be like

id='2345' name='frank', english

finally others will be like

id='7897' group='' name='maria',scottish

what I need to do is, if group='' or group is not in the string at all I need to add it somewhere before the comma setting it to the text after the comma so in the 2nd example above the line would become:

id='2345' name='frank' group='english',english

and the same in the last example which would become

id='7897' name='maria' group='scottish',scottish

This is going into a bash script. I can't actually delete the line and add to the end of the file as it relates to the following line.

I've used the following:

sed -i.bak 's#group=""##' file 

which deletes the group="" string so the lines will either contain group='something' or wont contain it at all and that works

Then I tried to add the group if it doesn't exist using the following:

sed -i.bak '/group/! s#,(.*$)#group="\1",\1#' file

but that throws up the error

sed: -e expression #1, char 38: invalid reference \1 on `s' command's RHS

EDIT by Ed Morton to create a single sample input file and expected output:

Sample Input:

id='1244' group='american' name='fred',american
foo
id='2345' name='frank', english
bar
id='7897' group='' name='maria',scottish

Expected Output:

id='1244' group='american' name='fred',american
foo
id='2345' name='frank' group='english',english
bar
id='7897' name='maria' group='scottish',scottish
sed -r "
    /group=''/ s///                                   # group is empty, remove it
    /group=/!  s/,[[:blank:]]*(.+)/ group='\\1',\\1/  # group is missing, add it
" file
id='1244' group='american' name='fred',american
foo
id='2345' name='frank' group='english',english
bar
id='7897'  name='maria' group='scottish',scottish

The foo and bar lines are untouched because the s/// command did not match a comma followed by characters.

Insert text at specific line number, You can use Vim in Ex mode: ex -s -c '3i|hello world' -c x file.txt. 3 select line 3. i insert text and newline. x write if changes have been made  The tutorial explains how to use the IF function in Excel. You will find a variety of IF formula examples for text, numbers and dates as well as IF statements for blank and non-blank cells.

something like

sed  '
    /^[^,]*group[^,]*,/ ! {
        s/, *\(.*\)/ group='\''\1'\'', \1/
    }
    /^[^,]*group='\'\''/ {
        s/group='\'\''\([^,]*\), *\(.*\)/group='\''\2'\''\1, \2/
    }
'

Python: add line to file if not already there, If there is no match add a new line to the end of the file i.e. “append a line to a file​”. python add-line-re.py lines.txt $ cat lines.txt line 1 line 2 line 3 line 4 You can do this manually, but Python's fileinput module does most of  This is going into a bash script. I can't actually delete the line and add to the end of the file as it relates to the following line. I've used the following: sed -i.bak 's#group=""##' file which deletes the group="" string so the lines will either contain group='something' or wont contain it at all and that works

This GNU awk may help:

awk -v sq="'" '
  BEGIN{RS="[ ,\n]+"; FS="="; found=0}
  $1=="group"{
    if($2==sq sq) 
      {next}
    else
      {found=1}
  }
  NF>1{
    printf "%s=%s ",$1,$2
  }
  NF==1{
    if(!found)
      {printf "group=%s",$1}
    print ","$1
    found=0
  }
' file

The script relies on the record separator RS which is set to get all key='value' pairs.

If the key group isn't found or is empty, it is printed when reaching a record with only one field.

Note that the variable sq holds the single quote character and is used to detect empty group field.

Unix Sed Tutorial: Append, Insert, Replace, and Count File Lines, The following example, appends the line “Website Design” at the end of the file. $ sed '$ a\ > Website Design' thegeekstuff.txt Linux Sysadmin  Hi, I'm looking for some help. I want to create a batch file that can find a particular line inside a text file and then insert a line below it and enter some text from the batch file.

Linux: Using sed to insert lines before or after a match – Fabian Lee , The sed utility is a powerful utility for doing text transformations. In this article, I will provide an example of how to insert a line before and after a  How can insert some text in specific lines of a file? What is most simplest method that I can use? (bash, sed, awk?) What I want to do, might be simple, but I don't know where I should start. It's takes too much time for me to try to do this manually (I have very a lot of files that I have to change for Excel/Calc later use).

Write text into first lines of every file, Note that I need to escape ! in my shell (Bash). Note that the insert option of sed doesn't work if the files are blank. If you write some text in the current folder's  This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression. This is primarily useful when you want to change a single line in a file only. See the replace module if you want to change multiple, similar lines or check blockinfile if you want to insert/update/remove a block of lines

Other Commands (sed, a stream editor), The text to add is read until the end of the line. The a command queues the lines of text which follow this command (each but the last Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication. To say that “WordPress line break not working is a known issue and we must live with it” is completely bezerk! Surely if I put a line break in the visual editor then it can be safely assumed that I do actually want it there. If I accidentally put a line break which I don’t want it is extremely easy to go into the visual editor and delete it.

Enable a text box to accept multiple lines of text, If you want a text box to be able to accommodate multiple lines of text, you can paragraph breaks or text wrapping for the text box after you insert it onto your form the Multi-line check box in a browser-compatible form template, you won't be  I've been looking around sed command to add text into a file in a specific line. This works adding text after line 1: sed '1 a\\ But I want to add it before line 1. It would be: sed '0 a\\ but I

Comments
  • What have you tried so far? Let's see some code :-)
  • Sorry I've edited my question to show what I have tried.
  • In BRE (Basic Regular Expression) syntax, the default pattern syntax for sed, parenthesis for a capture group have to be escaped: \(.*$\) otherwise, they are seen as literal characters.
  • I edited your question to try to provide 1 sample input file and the expected output given that input. If it's wrong then please fix it. A bunch of separate input lines, some with the expected output, some without, and none of it with the context of surrounding lines isn't nearly as useful as one concrete example with context.
  • This has pointed me in the right direction, it appears to be doing exactly as described but the EOL appears to be messing it up. I'm getting output like id='7897' name='maria' group='scottish so missing the last ' around the group and the text from comma onwards
  • could my latest issue be because I already use sed -i -e "s/^M//g" on the files?#
  • Kind of sounds like stay carriage returns. s/\r$//
  • With carriage returns, I expect you see the end of the line overwriting the start of the line: ',scottish name='maria' group='scottish