DaWe4444
DaWe4444

Reputation: 161

Cat file do some operation append from file output to first file

I'm trying to achieve the folowing:

  #!/bin/bash
  if [ -f file2 ]; then

  cat file2 > file1
  sed -i 's#some operation' file1
  cat file3 >> file1

  exit 0

  elif [ ! -f file2 ]; then

  cat file1 > file2
  sed -i 's#some operation' file1
  cat file3 >> file1

  exit 0

  else

  echo "something"
  exit 1

  fi

Any ideas how to do it simpler ? Without using that much cat and files ?

Thanks !

Upvotes: 2

Views: 169

Answers (2)

Gal Ben-Haim
Gal Ben-Haim

Reputation: 17803

sed -i 's# some operation' file1
cat file2 >> file1

Upvotes: 0

clt60
clt60

Reputation: 63892

For your original question:

cat file1 > file1.bak
sed -i 's# some operation' file1.bak
cat file2 >> file1.bak
echo -n > file1
cat file1.bak > file1
rm -f file1.bak

the simpler answer is (as I said in the comments):

sed -i 's# some operation' file1
cat file2 >> file1

For the edited question - bit more explanation.

In your condition:

#!/bin/bash
if [ -f file2 ]; then
...
elif [ ! -f file2 ]; then
...
else
...
fi

The else will never happenes. If the file1 exists and is a regular file. the if-then will run, and if you negate the above will run the elif-then. E.g. you can simplify it to

if [ -f file2 ]; then
...
else
...
fi

Now to the actions:

The:

cat file2 > file1
sed -i 's#some operation' file1
cat file3 >> file1

is the same as:

sed 's#some operation' <file2 >file1
cat file3 >> file1

and the:

cat file1 > file2
sed -i 's#some operation' file1
cat file3 >> file1

is OK - you creating a backup (copy) of the file1 to file2. This could be written also as cp file1 file2.

Comparing the two sections, you in both doing the same thing:

cat file3 >> file1

so, DRY (Don't Repeat Yourself) - and do this after the if, because it is common for both parts.

So, we get:

if [ -f file2 ]; then
    sed 's#some operation' <file2 >file1
else
    cp file1 file2  #make the copy
    sed -i 's#some operation' file1
fi
cat file3 >> file1

Also, the

sed 's#some operation' <file2 >file1
#and
sed -i 's#some operation' file1

are very similar - e.g. the result of the sed operations always going into the file1. Moreover, in the else you copying the file1 to file2 so the

cat file1 > file2
sed -i 's#some operation' file1

could be written also as

cp file1 file2
sed 's#some operation' <file2 >file1

and we got the indentical sed command for both cases, so - DRY again.

if [ -f file2 ]; then
    : #do nothing here...
else
    cp file1 file2  #make the copy
fi
sed 's#some operation' <file2 >file1
cat file3 >> file1

But the do nothing part is unnecessary, so we got:

if [ ! -f file2 ]; then
    cp file1 file2  #make the copy
fi
sed 's#some operation' <file2 >file1
cat file3 >> file1

this could be shortened using [[ condition ]] && ... but it is not needed for now. :)

Also it could be very helpful name your files more precisely. The file1 and so on - telling you nothing about the content.

Upvotes: 2

Related Questions