LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-29-2014, 11:13 PM   #1
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Rep: Reputation: 16
output of wc -L in variables


how to print the output of wc -L in two different variables.

One variable contains the length part and another contains the file name.
Is there any way to do it?
 
Old 01-29-2014, 11:26 PM   #2
casualfred
Member
 
Registered: Aug 2012
Location: Kentucky, USA
Distribution: Slackware
Posts: 97

Rep: Reputation: 27
I'm not sure if this is the answer you're looking for, but sometimes in scripts I do:
Code:
MYVAR=`cat theFile | wc -l`
That way you will end up with only the number.
 
Old 01-29-2014, 11:44 PM   #3
jone kim
Member
 
Registered: Apr 2010
Posts: 185

Original Poster
Rep: Reputation: 16
I think this will the whole output inside one variable. But I want to store the length part
of the output to be stored in one variable and file name in another variable.

How can I do it?
 
Old 01-29-2014, 11:54 PM   #4
casualfred
Member
 
Registered: Aug 2012
Location: Kentucky, USA
Distribution: Slackware
Posts: 97

Rep: Reputation: 27
That command will give you only the number.

Example where the longest line in theFile.txt is 18 characters:
Code:
$ ./myscript.sh theFile.txt
theFile.txt
18
Content of myscript.sh:
Code:
#!/bin/bash

FILENAME="$1"
echo $FILENAME

LONGESTLINE=`cat "$1" | wc -L`
echo $LONGESTLINE
 
Old 01-30-2014, 12:52 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274Reputation: 7274
first, do not use cat file | wc -L but wc -L file.
you can do the following:
Code:
a=$(wc -L filename)
line=${a#* }
lineno=${a% *}
 
  


Reply

Tags
command


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
storing the output in two different variables jone kim General 14 02-06-2014 04:14 AM
Adding variables then output to another variable stevo520 Linux - Newbie 2 07-06-2012 07:06 PM
[SOLVED] SQL Command output to bash variables chirayu11 Linux - Newbie 5 08-04-2010 07:40 AM
ambiguous output using variables in bash gwong Linux - Newbie 2 05-05-2007 08:49 PM
Scripting : Dual line output into next script as variables facets Programming 6 06-10-2004 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration