Bounce
Bounce

Reputation: 2095

Change content in a text file

I have text file that contains following content:

param_1=7412
param_2=1234

What I want to do is to set param_1 value to param_2 and set new value to param_1. So it should look like this:

param_1=9999
param_2=7412

After changing the values, I have to save the content back to file.

Maybe I could use "sed" command to implement this? Any more ideas?

Upvotes: 0

Views: 5037

Answers (4)

Saucier
Saucier

Reputation: 4360

I wasn't quite sure what the actual goal is. But I interpreted the goal to be to pop the last entry away and shift all other entries up.

Personally I do think there are better tools to do that kind of processing (e.g. python) but I tried to solve that puzzle with pure bash.

Here is what the script does:

$ cat input.txt
param1=1000
param2=2000
param3=3000
param4=4000
$ ./script input.txt 10000
$ cat input.txt
param1=10000
param2=1000
param3=2000
param4=3000

Here is the script, please see comments for details:

#!/usr/bin/env bash

# Take an input file as the first argument
data="$1"

# Take the new value as the second argument
new_value=$2

# Read each line of the input file into an array
# note that readarray is a bash 4 builtin command
readarray -t entries < "$data"

# Get the length of the array
len_entries=${#entries[@]}

# Remove the last entry of the array since we don't need it anymore
unset entries[len_entries-1]

# Set up a counter needed for indexing and creating the new string inside the loop
counter=2

# Process each entry of the array
for entry in "${entries[@]}"; do

    # Get the value of the param?=*
    entry="${entry#*=}"

    # Enumerate the new string
    entry="param${counter}=${entry}"

    # Write the new string to its new position inside the array
    entries[counter-1]="$entry"

    # Raise the counter
    (( counter++ ))
done

# Add the new user defined value to the first index of the array
entries[0]="param1=${new_value}"

# Write the contents of the array to the file
printf "%s\n" "${entries[@]}" > "$data"

Upvotes: 0

MLSC
MLSC

Reputation: 5972

Totally:

You could use sed command for doing that:

sed -i 's|param_1=7412|param_1=9999|' file.txt
sed -i 's|param_2=1234|param_2=7412|' file.txt

UPDATE

But in more detailed answer I wrote a bash for you:

#!/bin/bash                                               

VAL_1=`echo $RANDOM | cut -c1-4`   #VAL_1=`shuf -i 0000-9999 -n 1`
VAL_2=`awk -F= '{print $2}' file.txt | head -1`

sed -i.bak "1s/.*/\param_1=${VAL_1}/g" file.txt
sed -i.bak "2s/.*/\param_2=${VAL_2}/g" file.txt

Upvotes: 1

Blaise
Blaise

Reputation: 7518

To have more flexibility with the parameters values you can use can use the source command or . operator

Lets assume the file contains the following line

param_1=7412
param_2=1234

we can then import this variable using

#!/bin/bash

source <filename>
echo $param_1
echo $param_2

param_1=9999
param_2=7412

#check that new values are assigned to variables
echo $param_1
echo $param_2

Then use the echo command to put the new values to a text file. Mind that you should take care of the file being deleted before or you need to choose if you want it to be appended or overwritten:

destdir=/some/directory/path/filename

if [ -f "$destdir"]
then 
    echo "param_1=$param_1" >  "$destdir"
fi

The if tests that $destdir represents a file.

Note: The > replaces the text in the file. If you only want to append the text in $param_1 to the file existing contents, then use >> instead:

echo "param_2=$param_2" >> "$destdir"

Hope this helps.

References:

https://askubuntu.com/questions/367136/how-do-i-read-a-variable-from-a-file

Shell - Write variable contents to a file

Upvotes: 2

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25875

If you not know previous value then you can modify file as config way like

 #!/bin/bash
sed -i "s/\(param_1 *= *\).*/\19999/" file
sed -i "s/\(param_2 *= *\).*/\17412/" file

Upvotes: 2

Related Questions