I want to add headers and footers to a text file with Linux commands.

Asked 1 years ago, Updated 1 years ago, 329 views

I would like to process the text file (test.txt) using the Linux command as follows.

before processing:

--SQL

After processing:

header
-- SQL
footer

The following shell scripts did not work:
I would appreciate it if you could tell me how it works.

test.sh

#not found.
sed-i'1s/^/header\\n/'test.txt

# For some reason, the file name test.txt· is created.
echo footer>>test.txt

linux shellscript sed

2022-10-19 00:01

3 Answers

$sed-i-E-e'1 iheader'-e'$afooter'test.txt
$ cat test.txt
header
-- SQL
footer


2022-10-19 00:01

Another example of not using sed.

#!/bin/sh

echo "header" > > result.txt
cat test.txt>>result.txt
echo "footer" > > result.txt


2022-10-19 00:01

There is no big mistake in the code of the question.This code does not cause "not found" or "test.txt·" to be created.I guess I did something wrong when I was trying a lot of things.

A small mistake is \\n.\n is correct.(\n seems to be a GNU extension, but the -i option is a GNU extension in the first place, so I think it's a usable environment.)


2022-10-19 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.