Help with wc and line breaks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with wc and line breaks
# 1  
Old 08-05-2010
Help with wc and line breaks

Hi everyone,

I have gone through the forum trying to find an answer to this question but was unsuccessful. I am hoping that someone can help me with this please.

I am trying to get my script to recognise line breaks from a file and to give me a result for wc of each line. So basically, if you have this

This is one line
This is the second line
This is the third one

I need a wc output for all 3 lines separately. I was wondering if there is anyway to get my script to recognise a line break and to print a wc output for the line before the line break ?

I hope I am not being too confusing. Would really appreciate your help.

Thanks
SG
# 2  
Old 08-05-2010
If you know char count in your file for first 3 lines

Code:
./justdoit
1 line char count -> 16
2 line char count -> 23
3 line char count -> 21

Code:
 # justdoit
c=1
while read -r line
  do
    if [ $c -lt 4 ] ; then
      echo "$c line char count ->" `printf "$line"| wc -c`
    fi
    ((c++))
  done< infile

# 3  
Old 08-05-2010
Another approach:
Code:
awk '{print "Line "NR ": " NF}' file

# 4  
Old 08-05-2010
Thanks a lot guys Smilie ..

One more favor, this method of yours counts the number of words on each line .. Can we count the number of characters too ?

Thanks
# 5  
Old 08-05-2010
Quote:
Originally Posted by ygemici
If you know char count in your file for first 3 lines

Code:
./justdoit
1 line char count -> 16
2 line char count -> 23
3 line char count -> 21

Code:
 # justdoit
c=1
while read -r line
  do
    if [ $c -lt 4 ] ; then
      echo "$c line char count ->" `printf "$line"| wc -c`
    fi
    ((c++))
  done< infile

Hi, ygemici:

This solution has several bugs.

1. A default value of IFS will cause leading/trailing whitespace to be trimmed, resulting in an incorrect character count for lines which contain it. FIX:
Code:
while IFS= read -r line

2. A character sequence in $line that is also a valid printf format specifier will feed incorrect data to wc (in C this would be a massive security hole, but in sh it's comparitively harmless). FIX:
Code:
 printf %s "$line"

Regards,
Alister
# 6  
Old 08-05-2010
Actually,

I need to be able to count all characters on each line (before a line break), in a file and also to be able to remove all spaces in a line but not the line breaks, so

this is a line and
this is another one

should become

thisisalineand
thisisanotherone

Is this possible ? I have been googling for a while now and cant find a solution for both these problems.

Would really appreciate it.

Thanks
# 7  
Old 08-05-2010
One solution (yes I know it contains "cat" and "echo"). The "\c" in the echo line stops the linefeed being counted by "wc".


Code:
cat myfile | while read line1
do
        chars1=`echo "${line1}\c"|wc -c`
        echo "\"${line1}\" is ${chars1} characters long"
        #
        # Remove space characters from line
        line2=`echo "${line1}"|tr -d ' '`
        #
        chars2=`echo "${line2}\c"|wc -c`
        echo "\"${line2}\" is ${chars2} characters long"
done



"This is one line" is 16 characters long
"Thisisoneline" is 13 characters long
"This is the second line" is 23 characters long
"Thisisthesecondline" is 19 characters long
"This is the third one" is 21 characters long
"Thisisthethirdone" is 17 characters long

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove line breaks and extra spaces

Hi, I want to remove all extra spaces, line breaks . Need a new line entry only for term starting"array" For eg: my input is array(), array(), array(), and my expected output is array(), array(), array(), Is it possible using awk? (5 Replies)
Discussion started by: rsi.245
5 Replies

2. Linux

Line breaks in mail spool

Hi, I have an issue with the line breaks in the mail spool- /var/mail/user1. I have set up a script to go through the mail spool on one of the users and parse some parts of the mail, however there doesn't seem to exist the regular line endings CR, LF or both in the lines and this is breaking my... (4 Replies)
Discussion started by: night_watcher
4 Replies

3. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

4. HP-UX

After using @, line breaks for a particular user in shell

Dear Concern, When we using @ sign, line breaks for a particular user in shell. Please advise how to resolve from the problem in HP UX. tabs@tabsdb02:/ccbs/users/tabs$ cat /etc/passwd|grep tabs tabs:RdCgOsmKee7Ps:221:201::/ccbs/users/tabs:/usr/bin/ksh... (3 Replies)
Discussion started by: makauser
3 Replies

5. UNIX for Dummies Questions & Answers

Page breaks and line breaks

Hi All, Need an urgent solution to an issue . We have created a ksh file or shell script which generates 1 DAT file. the DAT file contains extract of a select statement . Now the issue is , when we are executing the ksh file , the output is coimng with page breaks and line breaks . We have... (4 Replies)
Discussion started by: Ayaskant
4 Replies

6. Programming

Clean and keep line breaks

Hello, I want to keep line spaces in comments but clean more then 2 after each. Example: $sentence="This is my first sentence This will be in a new row This will be too in a new row but not separated with 3line breaks just with one "; And i want to... (1 Reply)
Discussion started by: AimyThomas
1 Replies

7. Shell Programming and Scripting

Remove line breaks after a match

I need to remove all line breaks in a document after a match, until there is a blank line. Example below, after the match "THE GREEN TABLE" remove line breaks until a blank line. Then, after the match "THE BLUE TABLE" do the same. Before: THE GREEN TABLE Lorem ipsum dolor sit amet,... (14 Replies)
Discussion started by: dockline
14 Replies

8. Shell Programming and Scripting

any better way to remove line breaks

Hi, I got some log files which print the whole xml message in separate lines: e.g. 2008-10-01 14:21:44,561 INFO do something 2008-10-01 14:21:44,561 INFO print xml : <?xml version="1.0" encoding="UTF-8"?> <a> <b>my data</b> </a> 2008-10-01 14:21:44,563 INFO do something again I want... (3 Replies)
Discussion started by: csmklee
3 Replies

9. UNIX for Dummies Questions & Answers

Newbie ? Need Help with If/Then & Line Breaks...

I hope this makes sense, but I need help with what is indicated with %%, below : A.) ####List active servers and send to file#### # ps -ef | grep jboss | grep sh | awk '{if ($14) {print $12;}else {print $11}}' | sort > /export/home/kthatch/script_results client302 client306 client309... (1 Reply)
Discussion started by: kthatch
1 Replies

10. Shell Programming and Scripting

Removing line breaks from a shell variable

Here is my snippet of code... getDescription() { DESCRIPTION=$(dbaccess dncsdb - << ! 2>/dev/null|sed -e 's/hctt_description//' -e '/^$/ d'|tr -d '\r' select hct_type.hctt_description from hct_type,hct_profile where hct_type.hctt_id=hct_profile.hctt_id and... (5 Replies)
Discussion started by: lyonsd
5 Replies
Login or Register to Ask a Question