Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Header Row using sed

Tags:

sed

converter

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

But 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

like image 334
SolaceBeforeDawn Avatar asked Oct 04 '22 08:10

SolaceBeforeDawn


2 Answers

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.

like image 153
PsychoData Avatar answered Oct 10 '22 04:10

PsychoData


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
like image 33
SolaceBeforeDawn Avatar answered Oct 10 '22 03:10

SolaceBeforeDawn