Strip out number from wc output?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Strip out number from wc output?
# 1  
Old 07-23-2014
Strip out number from wc output?

I am trying to output two command substitutions echo "$(command) $(command)" with a single echo however when using wc -l within the second substitution, that substitution, instead of appearing in order at the end of the line output, it overwrites the beginning of the output line.

I've tried pushing the wc -l output through awk or cut however the overwriting persists.

Any suggestions on how I can definitively pull out the digit output of wc -l without any other control characters coming through into my output?

somegeek
# 2  
Old 07-23-2014
so the output seems to be of only 1 wc command? $() will only strip the last \n. it sounds like a \r but wc shouldn't output that. is this Windows or Cygwin?

if you do
Code:
echo "$(wc -l file) $(wc -l file)" | cat -e

do you get
Code:
0 file^M0 file$

if so try replacing wc with a function that'll strip that
Code:
wc() { command wc "$@" | sed 's/\r//'; }

# 3  
Old 07-24-2014
I'm currently performing this using a one liner in bash... I'm a bit of a script noob.

When I add in cat -e I now see the wc -l output(3 in this case) at the end of the line (woot - no more overlapping output) with the characters you mentioned:
Code:
^M       3$

---------- Post updated at 07:19 PM ---------- Previous update was at 07:09 PM ----------

I did some more poking and piping my echo statement to
Code:
tr -dc '[:print:]'

cleared things up. How can I go back and identify what was causing the issue or was it wc's inherent output formatting?

Appreciate the assitance.

---------- Post updated at 08:56 PM ---------- Previous update was at 07:19 PM ----------

Thinking something is getting through with the files I'm pulling from.

I perform this with some made up files with basic strings and all is well. No overlapping funkiness:
Code:
file1:
windows 7
application 1
application 2
application 3

file2:
windows 7
application 1
application 2
application 3
application 4
application 5



> ls * | while read a; do printf "$a $(egrep "windows" $a) $(egrep application $a | wc -l)\n";done
file1 windows 7       3
file2 windows 7       5

---------- Post updated at 09:43 PM ---------- Previous update was at 08:56 PM ----------

Figured this out with the files I'm working with... tr -d '\r' removed the carriage return but I wasn't sure where since it worked when I put it after the echo statement. Moved it back, one at a time and see it needed to remove that from the first command substitution. All good now. Nothing to do with wc from the look of it at this point... just my own skewed perception. Smilie

ls * | while read a; do printf "$a $(egrep "text" $a | tr -d '\r') $(egrep other_text $a | wc -l)\n";done

Thanks again.

Last edited by somegeek; 07-24-2014 at 12:32 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to output non-number lines with grep?

I want to check my data quality. I want to output the lines with non-number. I used the grep command: grep '' myfile.csv Since my file is csv file, I don't want to output the lines with comma. And I also don't want to output "." or space. But I still get the lines like the following:... (8 Replies)
Discussion started by: twotwo
8 Replies

2. Shell Programming and Scripting

awk to output lines less than number

I am trying to output all lines in a file where $7 is less than 30. The below code does create a result file, but with all lines in the original file. The original file is tab deliminated is that the problem? Thank you :). awk 'BEGIN{FS=OFS=","} $7 < 30 {print}' file.txt > result.txt... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. UNIX for Dummies Questions & Answers

The number of links, output of ls

hi: i am trying to understand the concept of the output of ls command, specifically "number of links" for a directory and got utterly confused: 1) when i made first directory in my home directory: dave@host:~:$ mkdir dir_l1 from the perspective of home directory, the link number for... (2 Replies)
Discussion started by: ipfreak
2 Replies

4. Shell Programming and Scripting

Getting awk Output as number instead of String

Hi All, I have a file a.txt, content as mentioned below: 22454750 This data in this control file and I have a variable called vCount which contains a number. I need to extract the 22454750 from the above file and compare with the variable vCount. If match fine or else exit. ... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

5. Shell Programming and Scripting

awk number output

Hi, I have a problem when doing calculations in awk. I want to add up a few numbers and output the result. testfile: 48844322.87 7500.00 10577415.87 3601951.41 586877.64 1947813.89 $ awk '{x=x+$1};END{print x}' testfile 6.55659e+07The problem is the number format. It should show... (3 Replies)
Discussion started by: Subbeh
3 Replies

6. Shell Programming and Scripting

Extract particular number from the command output

Hi Folks, I want to use particular number as a variable output..Please find the below for scenario... Example 1:- Below output i want to use secondary group 9003 as a variable output $ id -a |awk -NF '{print $3}' groups=99(local),9003(testadmin) Else I want to use 2006 as a... (8 Replies)
Discussion started by: susindram
8 Replies

7. Shell Programming and Scripting

find with given number and express output

file A eebbbeeeeee file B 4 Question is by file B and look into file A output is b awk -v v1=4 file B or something else (2 Replies)
Discussion started by: cdfd123
2 Replies

8. Shell Programming and Scripting

KSH Output/Strip portion of file in HP-UX

I have a file, we'll call it file.txt. It has thousands of lines of all kinds of output at any given time (ie. foo bar foo bar) I need to copy out just a portion of the file from Point-A to Point-B. I'd like to save off just that portion to a file called test123xyz.txt. How do I do that? ... (7 Replies)
Discussion started by: austin881
7 Replies

9. Shell Programming and Scripting

How to Strip lines off Streamed EDI Output

Attached is a streamed EDI ANSI X12 output where the segment terminator/delimiter is a tilde ~ character. Is it possible to do the following pseudo-code in a unix script (using either sed, awk and/or grep)? Open file StreamedOutput.txt Search for ISA and delete the data up to the tilde ~ char... (7 Replies)
Discussion started by: sapedi
7 Replies
Login or Register to Ask a Question