Sponsored Content
Top Forums Shell Programming and Scripting Bash - Appending to specific line in file Post 302793081 by Don Cragun on Thursday 11th of April 2013 04:13:16 PM
Old 04-11-2013
Quote:
Originally Posted by MadeInGermany
Assumed your filename is "file".
With sed:
Code:
factor="3,6"
trial="YES,18,1.635"
sed '/'"$factor"'|/ s/$/|'"$trial"'/' < file > file.new

With the shell built-ins it becomes a bit long,
so a function makes sense:
Code:
append () {
IFS="|"   
while read factor trial
do
  if [ "$factor" = "$1" ] 
  then
    echo "$factor|$trial|$2"
  else
    echo "$factor|$trial"
  fi
done < file > file.new
}

and call it
Code:
append "3,6" "YES,18,1.635"

---------- Post updated at 02:47 PM ---------- Previous update was at 02:38 PM ----------

The sed version has the problem that it does not have a true field separator.
Here is an awk version:
Code:
awk -F"|" '{if ($1==factor) {print $0 FS trial} else {print}}' factor="3,6" trial="YES,18,1.635" < file > file.new

Unfortunately, both of these examples only work if all possible values of factor are in file before you try to add a new trial. If you want to start with an empty file and add new entries (as well as update existing entries), you could try these minor updates:
Code:
append () {
IFS="|"
found=0
while read factor trial
do
  if [ "$factor" = "$1" ] 
  then
    echo "$factor|$trial|$2"
    found=1
  else
    echo "$factor|$trial"
  fi
done < file > file.new
if [ $found = 0 ]
then
    echo "$1|$2" >> file.new
fi
}

Code:
awk -F"|" '{if ($1==factor) {print $0 FS trial;found=1} else {print}END{if(! found) print factor FS trial}' factor="3,6" trial="YES,18,1.635" < file > file.new

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading specific contents from a file and appending it to another file

Hi, I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from, File 1 -----# more... (5 Replies)
Discussion started by: dnicky
5 Replies

2. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

3. Shell Programming and Scripting

Appending a column in one file to the corresponding line in a second

It appears that this has been asked and answered in similar fashions previously, but I am still unsure how to approach this. I have two files containing user information: fileA ttim:/home/ttim:Tiny Tim:632 ppinto:/home/ppinto:Pam Pinto:633 fileB ttim:xkfgjkd*&#^jhdfh... (3 Replies)
Discussion started by: suzannef
3 Replies

4. Shell Programming and Scripting

How to read the value from a specific line and column BASH

Hi All, I have the same problem as the one posted in https://www.unix.com/shell-programming-scripting/96097-how-read-value-specific-line-column-csh-variable.html but I'm using bash. Can anyone tell me how I have to modify the code to make it bash compatible? eval `awk 'NR==3{print "set... (5 Replies)
Discussion started by: f_o_555
5 Replies

5. UNIX Desktop Questions & Answers

Appending file extensions to filenames in bash scripts

Hi Suppose I have a variable called filename and it it contains the name of a file. I then would like to append an extension to that filename. The filename currently has no extensions. How do I do this? Thanks (11 Replies)
Discussion started by: ladyAnne
11 Replies

6. UNIX for Advanced & Expert Users

Appending # to the start of specific line in a properties file

Hi, I have the following file, ABC.txt: ABC=123 DEF=234 FGH=345 Based on my validation and conditional processing it is observed that i need to comment or append # before DEF=234 so the same file ABC.txt should look as follows ABC=123 #DEF=234 FGH=345 Sorry if its a... (6 Replies)
Discussion started by: mihirvora16
6 Replies

7. UNIX for Dummies Questions & Answers

Comparing to specific line in file bash script

Hi, I have a value stored in x and I need to compare it to the numbers in every other line of a file. The file contains alternating lines of numbers and letters: aaaa1111 AAAAAAAA bbbb2222 BBBBBBBB cccc3333 CCCCCCCC I need to compare x to the numbers in every other line without the... (2 Replies)
Discussion started by: ShiGua
2 Replies

8. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

9. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

10. Shell Programming and Scripting

Appending content of a file to another file before a specific character

Hi there, i've got a file with this content $ cat file1 Matt Mar The other file has the same number of lines with this content: $ cat file2 20404=767294 23450=32427 is there a way with either using sed, awk or paste to insert the content of file1 before the "=" character? So... (3 Replies)
Discussion started by: nms
3 Replies
All times are GMT -4. The time now is 08:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy