Avoiding new line for the counts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoiding new line for the counts
# 1  
Old 02-05-2018
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 #####

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:

Code:
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:

Code:
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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-05-2018 at 12:41 PM.. Reason: Added CODE tags.
# 2  
Old 02-05-2018
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?

Last edited by RudiC; 02-05-2018 at 01:14 PM..
# 3  
Old 02-05-2018
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:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 02-05-2018
Thanks Don and Rudic
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove duplicated records and update last line record counts

Hi Gurus, I need to remove duplicate line in file and update TRAILER (last line) record count. the file is comma delimited, field 2 is key to identify duplicated record. I can use below command to remove duplicated. but don't know how to replace last line 2nd field to new count. awk -F","... (11 Replies)
Discussion started by: green_k
11 Replies

2. Shell Programming and Scripting

Avoiding external utilities

under past circumstances, id be fine running commands like this: case 1: printf '%s\n' "${MASSIVETEXT}" | egrep "i am on the first line" case 2: vprintf '%s\n' "${MASSIVETEXT}" | egrep -v "i am on the first line" This works fine. Bit it calls external utility "egrep" and "printf". ... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. UNIX for Dummies Questions & Answers

Avoiding the history

In bash shell, how we can avoid the commands getting recorded in history file. One way i can think of is : export HISTSIZE=0 Is there any other way to achieve this? Thanks (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Shell Programming and Scripting

Avoiding 'sh -c' when running ps from CRON

Hi, I have a script which has the below line: ps -ef | grep ${SCRIPT_NAME} | grep ksh | grep -v grep >> /tmp/instance.tmp When the script is invoked through CRON, I get 2 lines in instance.tmp when actually only one instance is running: cdrd 17790 17789 0 15:14:01 ? 0:00 /bin/ksh... (8 Replies)
Discussion started by: cavallino4u
8 Replies

5. UNIX for Dummies Questions & Answers

Avoiding the second run of the script

Hi all, I want to put a check in my script to check if the same instance is already running and not finished and if not then does not allow it to run! in which part of my script I should put this? and any idea how I should write it? tx (4 Replies)
Discussion started by: messi777
4 Replies

6. Shell Programming and Scripting

word counts for a single line xml file

I have any XML ouput file(file name TABLE.xml), where the data is loaded in A SINGLE LINE, I need help in writting a ksh shell script which gives me the word counts of word <TABLE-ROW> This is my input file. <?xml version="1.0" encoding="UTF-8"?><!--Generated by Ascential Software... (4 Replies)
Discussion started by: pred55
4 Replies

7. Shell Programming and Scripting

Avoiding For Loop in Shell Script

I am looking to a solution to the following problem. I have a very large file that looks something like this: Each group of three numbers on each line are three probabilities that sum to one. I want to output the maximum for each group of three. So desired output would be: or... (6 Replies)
Discussion started by: hydrabane
6 Replies

8. Shell Programming and Scripting

using find but avoiding sparse files

I am no Unix administrator...I live in windows land. I wrote a script to find files of certain names and process them but was later advised to avoid checking sparse files since it would use up a lot of resources and the files I was looking for were not there. How do I avoid doing the find on... (3 Replies)
Discussion started by: shellFun
3 Replies

9. UNIX for Dummies Questions & Answers

wc -c counts 1 char more per line

Hi, this might be a basic question... why is that wc -c counts 1 more per line than what is there. for example, > cat dum1.txt 123 12 > wc -c dum1.txt 7 dum1.txt Thanks, Sameer. (4 Replies)
Discussion started by: ensameer
4 Replies
Login or Register to Ask a Question