Linux add one file to another

Bash: Inserting one file’s content into another file after the pattern

What should I use to realize this functionality on BASH?

I wrote the code, but it doesn’t work properly (why?):

 #!/bin/bash first_filename="$1" second_filename="$2" pattern="$3" while read -r line do if [[ $line=˜$pattern ]]; then while read -r line2 do echo $line2 done < $second_filename fi echo $line done < $first_filename 

@Emil Vikstrom: My code just output first_filename's content in each line of the second_filename. It's incorrect.

5 Answers 5

sed can do that without loops. Use its r command:

sed -e '/pattern/rFILE1' FILE2 
$ cd -- "$(mktemp -d)" $ printf '%s\n' 'nuts' 'bolts' > first_file.txt $ printf '%s\n' 'foo' 'bar' 'baz' > second_file.txt $ sed -e '/bar/r./first_file.txt' second_file.txt foo bar nuts bolts baz 

@l0b0 sed -e '1,/pattern/rFILE1' FILE2 will insert it only once but there is an edge case where pattern occurs on the first line. In that case use GNU sed: sed '0,/pattern/rFILE1' FILE2

For the "do it just once" request, sometimes ed , while more verbose, is clearer than sed: printf "%s\n" '/2222/r first_file.txt' '1,$p' q | ed second_file.txt 2>/dev/null -- to edit in-place, replace '1,$p' with 'w'

Using awk works as well.

To insert before the ###marker### line :

// for each of second_file.txt : // if matches regexp ###marker###, outputs first_file.txt. // **without any condition :** print awk '/###marker###/ < system ( "cat first_file.txt" ) >\ < print; >\' second_file.txt 

To insert after the ###marker###line :

// for each of second_file.txt : // **without any condition :** print // if matches regexp ###marker###, outputs first_file.txt. awk ' < print; >\ /###marker###/ < system ( "cat first_file.txt" ) >\' second_file.txt 

To replace the ###marker### line :

// for each of second_file.txt : // if matches regexp ###marker###, outputs first_file.txt. // **else**, print awk '/###marker###/ < system ( "cat first_file.txt" ) >\ !/###marker###/ < print; >' second_file.txt 

If you want to do in-place replacement, use a temp file for being sure the pipe doesn't start before awk has read the entire file; add :

> second_file.txt.new mv second_file.txt // (like "mv second_file.txt.new second_file.txt", but shorter to type !) 

If you want replacement inside of the line, (replacing just the pattern and keeping the rest of the line), a similar solution should be achievable with sed instead of awk.

Источник

How to append one file to another in Linux from the shell?

Solution 4: Try this command: Question: I have two file filea and fileB content content I want to merge content of fileA and fileB and FileA content should be the first line in the merged file I tried using and command. not not able to get required result any suggestions. My current hosts file looks like this: I would like it to look like this: I have tried a variety of commands using the append ( ) command.

Читайте также:  Настройка geany python linux

How to append one file to another in Linux from the shell?

I have two files: file1 and file2 . How do I append the contents of file2 to file1 so that contents of file1 persist the process?

Use bash builtin redirection (tldp):

The >> operator appends the output to the named file or creates the named file if it does not exist.

This concatenates two or more files to one. You can have as many source files as you need. For example,

Update 20130902
In the comments eumiro suggests "don't try cat file1 file2 > file1 ." The reason this might not result in the expected outcome is that the file receiving the redirect is prepared before the command to the left of the > is executed. In this case, first file1 is truncated to zero length and opened for output, then the cat command attempts to concatenate the now zero-length file plus the contents of file2 into file1 . The result is that the original contents of file1 are lost and in its place is a copy of file2 which probably isn't what was expected.

Update 20160919
In the comments tpartee suggests linking to backing information/sources. For an authoritative reference, I direct the kind reader to the sh man page at linuxcommand.org which states:

Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell.

While that does tell the reader what they need to know it is easy to miss if you aren't looking for it and parsing the statement word by word. The most important word here being 'before'. The redirection is completed (or fails) before the command is executed.

In the example case of cat file1 file2 > file1 the shell performs the redirection first so that the I/O handles are in place in the environment in which the command will be executed before it is executed.

A friendlier version in which the redirection precedence is covered at length can be found at Ian Allen's web site in the form of Linux courseware. His I/O Redirection Notes page has much to say on the topic, including the observation that redirection works even without a command. Passing this to the shell:

. creates an empty file named out. The shell first sets up the I/O redirection, then looks for a command, finds none, and completes the operation.

Note : if you need to use sudo , do this:

sudo bash -c 'cat file2 >> file1'

The usual method of simply prepending sudo to the command will fail, since the privilege escalation doesn't carry over into the output redirection.

Linux - How to append contents of multiple files into one, If you want to append contents of 3 files into one file, then the following command will be a good choice: cat file1 file2 file3 | tee -a file4 > /dev/null. It will combine the contents of all files into file4, throwing console …

Читайте также:  Install ntp on linux

How to merge/append one file content to another file using shell script

I have two file filea and fileB

PersonName Value1 Value2 Value3 
ALBERT check1 check1 check1 ALBERT check2 check2 check2 ALBERT check3 check3 check3 

I want to merge content of fileA and fileB and FileA content should be the first line in the merged file

I tried using paste and sort command. not not able to get required result any suggestions.

cat FileA > NewFile cat FileB >> NewFile 

In Unix/Linux you can use the command cat

cat file1.txt file2.txt > file3.txt 

This will put the contents of file1 and file2 into file3.

This will add the information from file1.txt to the information already existing in file2.txt

$ cat FileA PersonName Value1 Value2 Value3 $ cat FileB ALBERT check1 check1 check1 ALBERT check2 check2 check2 ALBERT check3 check3 check3 $ cat FileA | tee -a FileB ALBERT check1 check1 check1 ALBERT check2 check2 check2 ALBERT check3 check3 check3 PersonName Value1 Value2 Value3 

Linux - How to append the output to a file?, You may also use tee, if you want to redirect to both STDOUT and append results to a file. For example: echo "hello" | tee -a somefile.txt, where the -a flag stands for append. – Henrik. Oct 18, 2011 at 12:03.

Append line to /etc/hosts file with shell script

I have a new Ubuntu 12.04 VPS. I am trying to write a setup script that completes an entire LAMP installation. Where I am having trouble is appending a line to the /etc/hosts file. My current hosts file looks like this:

127.0.0.1 localhost Venus # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

I would like it to look like this:

127.0.0.1 localhost Venus 192.241.xx.xx venus.example.com venus # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 

I have tried a variety of sed commands using the append ( \a ) command. For some reason Ubuntu either just echoes the contents of the hosts file in terminal or does nothing at all. How would I properly inject the second line into the file with a bash script?

Make sure to use the -i option of sed .

-i[SUFFIX], --in-place[=SUFFIX] edit files in place (makes backup if extension supplied) sed -i "2i192.241.xx.xx venus.example.com venus" /etc/hosts 
echo "192.241.xx.xx venus.example.com venus" >> /etc/hosts 

would append the line at the end of the file, which could work as you expect.

Insert/Update Entry

If you want to programmatically insert/update a hosts entry using bash, here's a script I wrote to do that:

#!/bin/bash # insert/update hosts entry ip_address="192.168.x.x" host_name="my.hostname.example.com" # find existing instances in the host file and save the line numbers matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)" host_entry="$ $" echo "Please enter your password if requested." if [ ! -z "$matches_in_hosts" ] then echo "Updating existing hosts entry." # iterate over the line numbers on which matches were found while read -r line_number; do # replace the text of each line with the desired host entry sudo sed -i '' "$s/.*/$ /" /etc/hosts done /dev/null fi 

The script is intended for use with OS X but would work on linux as well with minor tweaking.

Читайте также:  Create script file in linux

If your in mac or you need sudo permission to this try this:

sudo -- sh -c -e "echo '192.34.0.03 subdomain.domain.com' >> /etc/hosts"; 

It will still ask you for password.

alternative way from @kainjow

echo '192.34.0.03 subdomain.domain.com' | sudo tee -a /etc/hosts 
echo "127.0.0.1 localhost `hostname`">./temp_hosts echo "192.241.xx.xx venus.example.com">>./temp_hosts cat /etc/hosts |tail -n +2 >>./temp_hosts cat ./temp_hosts > /etc/hosts rm ./temp_file 

Linux - Run a shell command when a file is added, This folder is connected to a website and the admin of the site has the ability to add pictures to this site. However, when a picture is added, I want a command to run resizing all of the pictures a directory. In short, I want to know how I can make the server run a specific command when a new file is added to a …

Append the output as new line in shell script

I have a log file log .txt which is having the data in below format

Name=abc Date=20140710 Name=xyz Date=20140715 Name=pqr Date=20140810 And so on 

I am fetching the data based on today's date and appending it to a log file in new line

today=$(date --date "+1 week" +%Y%m%d) grep $today log.txt $'\r' >> append_file.txt 

But when I am running the script, it is giving me exception like

: No such file or directory 

Also, in the append_file.txt , it is keeping the data as

log.txt:Name=abc Date=20140710 

Ideally it should keep only the data i.e.

Actually, my end point objective is mail the content of append_file.txt and I want the data line wise. like this

Name=abc Date=20140710 Name=mno Date=20140710 

At present, it is mailing the data in single line Name=abc Date=20140710 Name=mno Date=20140710

log.txt:Name=abc Date=20140710 

Because grep thinks that you're giving more than one file to work with.

The problem is $'\r' in this line:

grep $today log.txt $'\r' >> append_file.txt 
grep $today log.txt >> append_file.txt 

or if you need to insert \r at the end of each line:

grep $today log.txt | sed -e 's/$/\r/g' >> append_file.txt 

How to append one file to another in Linux from the shell?, The >> operator appends the output to the named file or creates the named file if it does not exist. cat file1 file2 > file3 This concatenates two or more files to one. You can have as many source files as you need. For example, cat *.txt >> newfile.txt Update 20130902 In the comments eumiro suggests "don't try cat file1 file2 > file1 ." Usage examplecat file2 >> file1Feedback

Источник

Оцените статью
Adblock
detector