Need Help in using count in while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help in using count in while loop
# 1  
Old 01-25-2010
Need Help in using count in while loop

Hi ,

I have a source file having structure as below
aaaaaa cvvvvvv
bbbbbb ddddddd
wwww cccccds

I want to create a new file in which each line contains two pipe separated characters eg first character till end of first space and then next 2 characters from same file
eq
aaaaaa |cv
bbbbbb |dd

I have used following code

Quote:
#!/bin/sh
cat source | while read line
do
var1=`echo $line |cut -c 1-11`
var2=" "
count=0

until ($count -le 11)
do
var1 = `echo$var1echo$var2`
count=`echo $var1|wc -c`
echo "count $count is "
done
count=0
var3=`echo $line |cut -c 12-13`
echo "$var1|$var3" >>outputfile
done

Issue is the code is not considering spaces while checking for first character
Plz guide
Thanks
# 2  
Old 01-25-2010
Hi.

Does this have to be, specifically, a shell script?

In awk
Code:
$ awk '{print $1, "|" substr($2,1,2)}' source> newfile

$ cat source
aaaaaa cvvvvvv
bbbbbb ddddddd
wwww cccccds

$ cat newfile
aaaaaa |cv
bbbbbb |dd
wwww |cc

Or sed
Code:
sed "s/\(.*\) \(..\).*/\1 |\2/" source

Code:
#!/bin/sh
while read A B
do
  echo "$A |$(echo $B | cut -c1-2)"
done < file1


Last edited by Scott; 01-25-2010 at 12:03 PM..
# 3  
Old 01-25-2010
Or:
Code:
awk '$2="|"substr($2,1,2)' source

# 4  
Old 01-25-2010
Using your script as a starter and correcting some issues:

Code:
cat source | while read line
do
        var1=`echo "${line}"|cut -f1 -d" "`
        var2=`echo "${line}"|cut -f2 -d" "`
        var3=`echo ${var2}|cut -c1-2`
        echo "${var1} |${var3}" >> outputfile
done

aaaaaa |cv
bbbbbb |dd
wwww |cc


The key issue is to preserve space characters in the variable $line by surrounding it with double quote characters. We can then use "cut -f" to separate the line into two variables. Then we use "cut -c" to get the first two characters of the second variable. Finally we combine the components complete with the pipe character with one final echo - ensuring that the whole lot is between a pair of double quote characters.
# 5  
Old 01-25-2010
Hello,

In addition to the great answers , Something using perl and regexes - >

Code:
gaurav@localhost:~$ echo 'aaaaaa cvvvvvv
bbbbbb ddddddd
wwww cccccds' 
aaaaaa cvvvvvv
bbbbbb ddddddd
wwww cccccds
gaurav@localhost:~$ echo 'aaaaaa cvvvvvv
bbbbbb ddddddd
wwww cccccds' | perl -wln -e 'print $1," ","|",$2 if /^([a-z]+)\s([a-z]{2}).*$/'
aaaaaa |cv
bbbbbb |dd
wwww |cc
gaurav@localhost:~$

Regards,
gaurav.
# 6  
Old 01-25-2010
Thanks a lot to all for so prompt replies....
I got my solution by using "" to save the spaces which was exactly what i wanted...

Cheers

---------- Post updated at 11:01 AM ---------- Previous update was at 10:49 AM ----------

I also tried to use SQL Loader by using poistion identifier .
Even it was also not considering spaces. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find the count of IP addresses that belong to different subnets and display the count?

Hi, I have a file with a list of bunch of IP addresses from different VLAN's . I am trying to find the list the number of each vlan occurence in the output Here is how my file looks like 1.1.1.1 1.1.1.2 1.1.1.3 1.1.2.1 1.1.2.2 1.1.3.1 1.1.3.2 1.1.3.3 1.1.3.4 So what I am trying... (2 Replies)
Discussion started by: new2prog
2 Replies

2. Shell Programming and Scripting

Process a file for line count using for loop in awk

Hi, I have a file with contents So what I have to do is In short, break the file after every 6 lines and then truncate new line to tab for these 6 lines. I am not able to execute the for loop in awk properly. The idea is something like this: less file| awk '{for .... {if ((nr>=1)... (7 Replies)
Discussion started by: rossi
7 Replies

3. Shell Programming and Scripting

Getting the iteration count in WHILE LOOP

bash in RHEL 6.4 I have a requirement in which I want to get the iteration count from a WHILE LOOP. The below mentioned simple script test.sh works fine. In the below script, the WHILE loop will iterate every 5 seconds infinitely until it greps the string BASKETBALL from /tmp/somestring.txt... (6 Replies)
Discussion started by: John K
6 Replies

4. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

5. Shell Programming and Scripting

Record count checking for multiple files through for-loop

Hi Friends, I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count. What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's... (13 Replies)
Discussion started by: victory
13 Replies

6. Shell Programming and Scripting

Compare file1 header count with file2 line count

What I'm trying to accomplish. I receive a Header and Detail file for daily processing. The detail file comes first which holds data, the header is a receipt of the detail file and has the detail files record count. Before processing the detail file I would like to put a wrapper around another... (4 Replies)
Discussion started by: pone2332
4 Replies

7. Shell Programming and Scripting

Loop renaming files w/ a count problem

:wall: Hello there, basically in my program where im stuck at is when it comes to rename the files in a loop. - the program counts the number of files w a given name (works!) - and then if the number of files is greater or equal to the MAX_VERSIONS (numbers of files allowed w the... (1 Reply)
Discussion started by: thurft
1 Replies

8. Shell Programming and Scripting

The loop was executed $count times

#!/bin/sh count=0 for i in 2 4 6 do echo "i is $i" count='expr $count + 1' done echo "The loop was executed $count times" with these scripts my output is : i is 2 i is 4 i is 6 The loop was executed expr $count + 1 times What should I do to get the value instead of 'expr... (17 Replies)
Discussion started by: ymwong
17 Replies

9. Shell Programming and Scripting

How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus, I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script.... How can I do this..... (7 Replies)
Discussion started by: ustechie
7 Replies

10. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies
Login or Register to Ask a Question