Jump to content

Hi,

I'm trying to increment the following code to line 2 on file2.log, as I would like it to automatically print each line from file1.log, one at a time:

#!/bin/bash
let "LINE++"
( head -1 file2.log
  head -${LINE} file1.log | tail -1
  tail --lines=+2 file2.log
) > file2.new

mv file2.log file2.old
mv file2.new file2.log

How could this be done?

Thank you

Link to comment
https://linustechtips.com/topic/739472-bash-script-increment-a-variable/
Share on other sites

Link to post
Share on other sites

So you just want to take the text from the file, line by line, and append this to a log file?

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

2 minutes ago, spbr said:

No, I want to print it into a file onto a specific line. 

Does the file already exist, and, if so, does it already contain data?

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

3 minutes ago, spbr said:

correct

Do you want to replace the contents of that line, or append the data to the end of the line?

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

22 minutes ago, spbr said:

So within the file I would like to keep:

wireless-key s: 

and only replace the password everytime through the loop

Try this:

 

Use a while loop, which increments $lineNumber each time, until

wc -l file.txt

is reached and then runs

 

sed -i "$lineNumber s/.*/wireless-key s: $lineContents/" targetFilePath

 

You won't learn as much if I give you something to copy-paste :)

 

 

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

34 minutes ago, Fetzie said:

Try this:

 

Use a while loop, which increments $lineNumber each time, until

 


wc -l file.txt

 

is reached and then runs

 

 


sed -i "$lineNumber s/.*/wireless-key s: $lineContents/" targetFilePath

 

 

You won't learn as much if I give you something to copy-paste :)

 

 

 

Thank you

Link to post
Share on other sites

2 hours ago, Fetzie said:

Try this:

 

Use a while loop, which increments $lineNumber each time, until

 


wc -l file.txt

 

is reached and then runs

 

 


sed -i "$lineNumber s/.*/wireless-key s: $lineContents/" targetFilePath

 

 

You won't learn as much if I give you something to copy-paste :)

 

 

 

Hi,

 

I'm unsure on what to put in between the while loop brackets:

 

#!/bin/bash
x=1

while [ $x -le ]
do
        (       head -1 file2.log
                head -${x} file1.log | tail -1
                tail --lines=+2 file2.log
        ) > file2.new
        x=$(( $x + 1  ))
        if ( wc -1 file1.log  )
        then
                sed -i "$x s/.*/wireless-key s: $file1.log/" /etc/network/interfaces
                break
        fi
done

mv file2.log file2.old
mv file2.new file2.log
 

Link to post
Share on other sites

Why not something like:

 

#!/bin/bash 

sourceFile="/path/to/source/file"
targetFile="/etc/network/interfaces"
numLines=$(wc -l ${sourceFile})
counter=0

# exit if file is empty or does not exist, exit
if (( counter >= ${numLines} || ! -f ${sourceFile} )); then
	echo "invalid file"
    exit 0
fi   

while [ ${counter} -le ${numLines} ]; do

	sed -i "${lineNumber} s/.*/wireless-key s: $(sed -n ${counter}p <<< "${sourceFile}")/" "${targetFile}"
	counter=$((counter + 1))

done

 

 

Haven't run it but it should work. Might need bit bit of tweaking to get exactly what you want.

 

AMD Ryzen 7800 X3D, MSI B650 Project Zero, Antec C5, Gigabyte RTX 4080 Super Aero

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to post
Share on other sites

25 minutes ago, Fetzie said:

Why not something like:

 


#!/bin/bash 

sourceFile="/path/to/source/file"
targetFile="/etc/network/interfaces"
numLines=$(wc -l ${sourceFile})
counter=0

# exit if file is empty or does not exist, exit
if (( counter >= ${numLines} || ! -f ${sourceFile} )); then
	echo "invalid file"
    exit 0
fi   

while [ ${counter} -le ${numLines} ]; do

	sed -i "${lineNumber} s/.*/wireless-key s: $(sed -n ${counter}p <<< "${sourceFile}")/" "${targetFile}"
	counter=$((counter + 1))

done

 

 

Haven't run it but it should work. Might need bit bit of tweaking to get exactly what you want.

 

 

Thank you

Link to post
Share on other sites

On 2/18/2017 at 3:25 PM, Fetzie said:

Huge Thank you on the below

On 2/18/2017 at 3:25 PM, Fetzie said:

 



#!/bin/bash 

sourceFile="/path/to/source/file"
targetFile="/etc/network/interfaces"
numLines=$(wc -l ${sourceFile})
counter=0

# exit if file is empty or does not exist, exit
if (( counter >= ${numLines} || ! -f ${sourceFile} )); then
	echo "invalid file"
    exit 0
fi   

while [ ${counter} -le ${numLines} ]; do

	sed -i "${lineNumber} s/.*/wireless-key s: $(sed -n ${counter}p <<< "${sourceFile}")/" "${targetFile}"
	counter=$((counter + 1))

done

 

 

Haven't run it but it should work. Might need bit bit of tweaking to get exactly what you want.

 

1

 

Haven't run it but it should work. Might need bit bit of tweaking to get exactly what you want.

 

1

Unfortunately im at an inpass with the below:

sed -i "${lineNumber} s/.*/wireless-key s:" "$(sed -e ${counter}"p <<< "${sourceFile}")" ${targetFile}"

I'm unable to get this line correct so there is no errors, but when there isn't the target file doesn't print the contents of file1.log.

 

Thank you

Link to post
Share on other sites

7 minutes ago, sgzUk74r3T3BCGmRJ said:

It's unclear to me what you're trying to accomplish. You may get better help by explaining exactly want rather than asking for help doing whatever it is you think is the correct way to solve the problem. It could be that you've settled on the incorrect tools for the task and so asking for help banging them together is a waste of everyone's time.

 

Ignoring all of this BASH nonsense for a moment, is it fair to say you have two files?


$ cat foofile
flim
flam
floom

$ cat barfile
bing
bang
bong

Is it also fair to say you want to combine them by inserting the content of `foofile` into `barfile` at some arbitrary line?


$ combine-at-line 2 foofile barfile
flim
bing
bang
bong
flam
floom

If not then please give some examples of your source data and the expected output. Based on the above assumption, what you're describing looks like a job for `cat` and a file descriptor or two.

 

Hi,

I've got a list of permutations in one file and the program will need to do the following in this order:

  • Take one permutation
  • print to /etc/network/interfaces file, on line 3, in front of 'wireless-key s:'
  • restart the network
  • ping the network 5 times
  • - if successful save the file and print to a file or screen listing the password
  • if not successful restart from the top 

Thank you

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×