Wednesday, April 24, 2019

Adding/pre-append the text in Linux files

To add text to a starting of a file, use sed command:

sed -i '1i Header of the file' orginal_file.txt

Here, 1i mentions the following string need to be added to first line of the file

If you have multiple files to do the same thing, a small shell script with execute permission should be fine.

Example: 

for file in *.csv; do
        sed -i "1i Adding this to the starting of the file." "$file"
done

Caution: If you try to add header to all files after split, then the first file will be having duplicate header.

***

No comments:

Post a Comment