Avoiding new line for the counts

Hi Team,

Am getting the below output but need the count of records to be displayed in same line but currently count alone moves to next line. Please let me know how we can still keep the count in the same line.

######code #####

while read YEAR; do
for i in TEST_*PGYR${YEAR}_${DT}.csv; do
    printf "%s\n" "Number of transactions in $i : "`cat $i | tail +2 | wc -l`  >> ${LOG_FILE}  
    done
done < dynamic_years.txt

######code #####

Current Output:

Number of transactions in TEST_PGYR2013_02052018.csv : 
18
Number of transactions in TEST_PGYR2014_02052018.csv : 
179
Number of transactions in TEST_PGYR2015_02052018.csv : 
166

Desired Output:

Number of transactions in TEST_PGYR2013_02052018.csv : 18
Number of transactions in TEST_PGYR2014_02052018.csv : 179
Number of transactions in TEST_PGYR2015_02052018.csv : 166

Look at your printf 's format string ...

EDIT: Looking again at your sample code, are you sure there's no space between the double quote after the colon and the first backtick when the strange behaviour occurs?

This is another example of why you should always tell us what operating system and shell you're using when you post a question in this forum.

From what I have seen in other threads in this forum recently, you might get the output you wanted with your code if you were running it on a Linux system. You certainly will not get what you want (and will get what you are seeing) if you run it on a UNIX or BSD system.

Either of the following should work reliably on any of these systems:

printf "%s\n" "Number of transactions in $i : `cat $i | tail +2 | wc -l`" >> ${LOG_FILE}

(which may produce varying numbers of spaces between the colon and the following number) or:

printf 'Number of transactions in %s : %d\n' "$i" `tail +2 "$i" | wc -l`  >> ${LOG_FILE}

(which will only put one space between the colon and the number of lines in the truncated file).

It would also be more efficient if you moved the redirection from the printf to the end of the outer loop.

1 Like

Thanks Don and Rudic