LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-14-2016, 03:08 PM   #1
kenw232
Member
 
Registered: May 2006
Posts: 125

Rep: Reputation: 12
Find length of fist line of all txt files


I want to loop through all the .TXT files in a directory and get the length of the first line of each one.

find /path -name "*.txt" -exec head -n1 {} \;
will print the first line of each file, now how do I print the LENGTH of the first line of each file? like "300" for 300 characters long.
 
Old 09-14-2016, 03:20 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Use wc

Code:
echo hello | wc -c
6
Code:
       -c, --bytes
              print the byte counts
 
Old 09-14-2016, 03:24 PM   #3
kenw232
Member
 
Registered: May 2006
Posts: 125

Original Poster
Rep: Reputation: 12
ya buts its counting all the output together

Code:
find . -name "*.php" -exec head -n1 {} \; | wc -c
1364901
 
Old 09-14-2016, 03:30 PM   #4
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
You can use a while loop to pipe each line found with find

Code:
while read i;
do head -n1 "$i" | wc -c
done < <(find . -name \*.php)
You can also use parallel

Code:
parallel 'head -n1 {} | wc -c' ::: \*.php
 
Old 09-14-2016, 03:35 PM   #5
kenw232
Member
 
Registered: May 2006
Posts: 125

Original Poster
Rep: Reputation: 12
This is good. Now I just need a condition if the length from wc is equal to say 400 to do something. How do I tell this to do a sed command if the length of the first line is 400 characters?

PHP Code:
while read i;
do 
head -n1 "$iwc -c
done 
< <(find . -name \*.php
 
Old 09-14-2016, 03:42 PM   #6
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
This can easily be done with a if conditional expression.

Feel free to use man bash and search for CONDITIONAL EXPRESSIONS

Here's a search to get you started

eg this guide
 
Old 09-14-2016, 03:50 PM   #7
kenw232
Member
 
Registered: May 2006
Posts: 125

Original Poster
Rep: Reputation: 12
yes I can do a conditional expression. but how to I capture the number of wc into a variable to check?
 
Old 09-14-2016, 03:59 PM   #8
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
You can use command substitution for that.

Code:
variable=$(echo hello | wc -c)

echo $variable
6
 
Old 09-14-2016, 04:04 PM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Couting characters, words, lenght of the words and total lenght in a sentence

and
https://www.google.com/#q=count+word...in+bash+script

Read.
Study.
Try.
Question.
 
Old 09-18-2016, 10:40 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Code:
find /path -name "*.txt" |
while IFS= read -r file
do
  read line <"$file"
  if [[ ${#line} -eq 400 ]]
  then
    echo "${file}: line1 is 400"
  fi
done
 
  


Reply


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
Add a FileID and delete one line for 900 txt files Migasi Linux - Newbie 2 03-04-2014 03:52 PM
[SOLVED] awk question - read in txt files, offset data by given amount, output new txt files pomico Programming 19 09-17-2012 11:43 AM
Extracting second line from multiple txt files and append to a file sal_x_sal Linux - Newbie 11 08-12-2012 04:00 PM
How do you find the maximum length of a command line argument? BrianK Linux - General 1 06-10-2008 04:53 AM
Command line tool to print length of mp3 files? J_Szucs Linux - Software 3 05-04-2004 05:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:29 AM.

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