0

My goal is to create a bash file that counts the lines in about 16 files, each having a date, and send the following (16 times) to a .csv file.

So far, I've managed to count the lines, but the .csv file is messy and needs to be trimmed and made suitable for csv: Ex: 1500 FNB_EgTrans_20171004.txt

My goal is to make it look like this: 1500,EgTrans,20171004

Can someone explain to me why my below method of using tr with cat isn't working as expected? Thanks. cat $reccnt | tr ' FNB_' ',' | tr '201' ',' < $reccnt

Below is the entire code in case elaboration is necessary:

#!/bin/bash
cd /ssinput/NY_AML/
reccnt=ny_row_count.csv
activedate='100000'
if [ -e $reccnt ];
then
rm $reccnt
else
touch $reccnt
chmod 755 $reccnt
fi
ls  -t1 FNB*|head -15 |cut -c 13- > ny_activedate.txt #second tail or cut?
activedate=$(cat ny_activedate.txt)
echo $activedate
wc -l *$activedate | head -16 > $reccnt
reccnt="${reccnt##*( )}"
cat $reccnt | tr 'FNB' ',' | tr '201' ',' <  $reccnt`
CK37
  • 11
  • 4
  • 5
    `tr` translates characters, not words. Just like it says in the man page. – Ignacio Vazquez-Abrams Oct 16 '17 at 21:39
  • 2
    *Useless use of cat* detected. And yes, use *sed*. – 0andriy Oct 16 '17 at 21:44
  • 1
    In the expression `... | tr '201' ',' < $reccnt`, from where do you expect `tr` to get its input? Will it read from the pipe, or from the file? – William Pursell Oct 16 '17 at 21:52
  • 1
    In order to prevent any *cat faux pas*, I tried using `sed 's/ FNB_/,/g' ny_row_count.csv`. However, it doesn't seem to be saving the file when closing. Also, is it ok to join the two sed's using a pipeline? Thanks. – CK37 Oct 16 '17 at 22:05
  • 1
    Use `sed` option `-i` for editing the input file. – Walter A Oct 16 '17 at 22:07
  • @William, my understanding was originally that it was required to send it to $reccnt in order to save it by using the `>`, but when I did that, nothing happened. So I think I erroneously reversed it. However, using `sed` might fix my issue if I can just get it to save the file after replacing it. – CK37 Oct 16 '17 at 22:08
  • @Walter, when I tried using `-i` it originally said **sed: illegal operation --i**. – CK37 Oct 16 '17 at 22:10
  • For those wondering, typing in `uname -a` showed the following `SunOS 5.11 11.3 sun4v sparc sun4v`, which wiki shows is associated with Solaris. I'm trying to look at the following but it mentions using Perl: [sed-i-what-the-same-option-in-solaris](https://stackoverflow.com/questions/3576380/sed-i-what-the-same-option-in-solaris/4314796#4314796) – CK37 Oct 16 '17 at 22:21
  • 1
    Do not tag as Linux when it is Sun. The `-i` option is for GNU sed, not the default Sun sed. – Walter A Oct 16 '17 at 22:23
  • Duly noted. Thanks @WalterA. – CK37 Oct 16 '17 at 22:25
  • I've tried adding the following, but it makes the csv file blank: `sed s/" FNB_"/","/g ny_row_count.csv > ny_row_count.csv sed s/"_201"/",201"/g ny_row_count.csv > ny_row_count.csv` – CK37 Oct 16 '17 at 22:42
  • @CK37, correct, you can't use the same file for input and output (`>` truncates the file when it starts operation, meaning there's nothing left for the read). Save your output to a different file, and rename it when complete. – Charles Duffy Oct 16 '17 at 22:47
  • @CK37, ...note that `sed -i` does that exact same thing (saving to a different file and renaming), so you're just doing the same operation by hand. – Charles Duffy Oct 16 '17 at 22:53
  • Many thanks, @CharlesDuffy! As a neat bonus, the fact that I have to do two sed operations means that I can rename the final iteration back to the original. ;) – CK37 Oct 16 '17 at 23:05
  • `sed -e 'op1' -e 'op2'` is generally preferable to `sed -e 'op1' | sed -e 'op2'`. Note that `mv` is an atomic operation at a filesystem level -- it either happens or it doesn't -- whereas overwriting a file will leave partial contents behind if interrupted (or if something tries to read the file while it's still running). – Charles Duffy Oct 16 '17 at 23:07

1 Answers1

0

You can change

inputfile=ny_row_count.csv
sed 's/ FNB_/,/g'   ${inputfile} > ${inputfile}.tmp
sed 's/_201/,201/g' ${inputfile}.tmp > ${inputfile}
rm ${inputfile}.tmp 

into

inputfile=ny_row_count.csv
sed -e 's/ FNB_/,/g' -e 's/_201/,201/g' ${inputfile} > ${inputfile}.tmp
mv ${inputfile}.tmp ${inputfile}

or

inputfile=ny_row_count.csv
sed 's/ FNB_/,/g; s/_201/,201/g' ${inputfile} > ${inputfile}.tmp
mv ${inputfile}.tmp ${inputfile}
Walter A
  • 19,067
  • 2
  • 23
  • 43