SolaceBeforeDawn
SolaceBeforeDawn

Reputation: 1053

Insert Header Row Using Sed

I need to run a bash script via cron to update a file. The file is a .DAT (similar to csv) and contains pipe separated values. I need to insert a header row at the top.

Here's what I have so far:

#!/bin/bash
# Grab the file, make a backup and insert the new line
sed -i.bak 1i"red|blue|green|orange|yellow" thefilename.dat

Exit

How can I save the file as a different file name so that it always takes fileA, edits it, and then saves it as fileB?

Upvotes: 6

Views: 15847

Answers (4)

Jon Thoms
Jon Thoms

Reputation: 10759

Unfortunately, the above answers didn't work for me when I encountered this on my MacOS laptop when piping output (i.e. no actual file), so I'm adding my own answer here that can be written using a single line:

./someScript.sh | sed '1i\'$'\n''MY_HEADER'$'\n'

Note that this option essentially inserts newline characters into your output. This answer was modified from another answer I found on a similar question on this subject.

Hopefully this helps somebody else having the same problem I had.

Upvotes: 0

elcortegano
elcortegano

Reputation: 2694

I found more clear an consistent the syntax with 'i' between the two single quotes for the pattern to 'insert'.

You can simply add a header, and save it in a different file with:

sed '1i header' file > file2

In your case:

sed '1i red|blue|green|orange|yellow' file > file2

If you wanted to save it on the same file, you'd use -i option:

sed -i '1i red|blue|green|orange|yellow' file

Upvotes: 0

SolaceBeforeDawn
SolaceBeforeDawn

Reputation: 1053

In case anyone finds it useful, here is what I ended up doing....

Grab the original file, convert it to the desired file type, whilst inserting a new header row and making a log of this.

#!/bin/bash -l
####################################
#
# This script inserts a header row in the file $DAT and resaves the file in a different format
#
####################################

#CONFIG

LOGFILE="$HOME/bash-convert/log-$( date '+%b-%d-%y' ).log"
HOME="/home/rootname"

# grab original file
WKDIR="$HOME/public_html/folder1"
# new location to save
NEWDIR="$HOME/public_html/folder2"

# original file to target
DAT="$WKDIR/original.dat"

# file name and type to convert to
NEW="$NEWDIR/original-converted.csv"


####################################

# insert a new header row
HDR="header-row-1|header-row-2|header-row-2 \r"

# and update the log file
{
echo "---------------------------------------------------------" >> $LOGFILE 2>&1
echo "Timestamp: $(date "+%d-%m-%Y: %T") : Starting work" >> $LOGFILE 2>&1
touch "$LOGFILE" || { echo "Can't create logfile -- Exiting."  && exit 1  ;} >>"$LOGFILE"

# check if file is writable
sudo chmod 755 -R "$NEW"
echo "Creating file \"$NEW\", and setting permissions."
touch "$NEW"  || { 
echo "Can't create file \"$NEW\" -- Operation failed - exiting" && exit 1   ;}

} >>"$LOGFILE" 2>&1

{
echo "Prepending line \"$HDR\" to file $NEW."
{ echo "$HDR" ; cat "$DAT" ;} > "$NEW"

 {   
if [ "$?" -ne "0" ]; then
echo "Something went wrong with the file conversion."
exit 1

else echo "File conversion successful. Operation complete."
fi
}

} >>"$LOGFILE" 2>&1
exit 0

Upvotes: 1

PsychoData
PsychoData

Reputation: 1304

do you really rename the old one to xxx.bak or can you just save a new copy?

either way, just use redirection.

sed 1i"red|blue|green|orange|yellow" thefilename.dat > newfile.dat

or if you want the .bak as well

sed 1i"red|blue|green|orange|yellow" thefilename.dat > newfile.dat \
&& mv thefilename.dat thefilename.dat.bak`

which would create your new file and then, only if the sed completed sucessfully, rename the orig file.

Upvotes: 5

Related Questions