SH script to parse string and return multi-line file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SH script to parse string and return multi-line file
# 1  
Old 03-15-2012
Question SH script to parse string and return multi-line file

Hello all, I have been asked to exercise my shell scripting and it has been 10 plus years since I used to do it so I can not remember hardly anything and ask for your help.

What I need to do is copy a line out of a file that can be 10 to 100 characters long, I then need to parse this line into groups of ten and then each group of ten I need to convert them into the binary 1/0s.

So far I have been using the cut command but this is very dirty way of doing it and would like someone to kick start my memory Smilie

Code:
#!/bin/sh
K="/tmp/test1"
L="/tmp/test2"
M="/tmp/test3"
N="/tmp/test4"
Z="tmp/binStr"
grep -i -w ${USER1} ${a} | cut -d: -f2 >> $K
grep -i -w ${USER2} ${a} | cut -d: -f2 >> $L
\cp /tmp/TMP_MENU_$$ ${a}
cat $k | cut -c 1-10 >> $M
cat $K | cut -c 11-20 >> $M
cat $K | cut -c 21-30 >> $M
cat $K | cut -c 31-40 >> $M
cat $K | cut -c 41-50 >> $M
cat $K | cut -c 51-60 >> $M
cat $K | cut -c 61-70 >> $M
cat $K | cut -c 71-80 >> $M
cat $K | cut -c 81-90 >> $M
cat $K | cut -c 91-100 >> $M
echo "ibase=16;obase=2;<Please tell me I can use a Variable here?>" | bc >> $Z
#Then the same thing for $L >> $N

After I figure this out then the fun part happens, I have to do a logical OR against the two files and then convert the binary back to an integer and then put them back into 1 long string, but 1st things first. Any help is drastically appreciated.
# 2  
Old 03-16-2012
this thread might give you a good starting point.

Also, you could replace this:
Code:
cat $K | cut -c 1-10 >> $M
cat $K | cut -c 11-20 >> $M
cat $K | cut -c 21-30 >> $M
cat $K | cut -c 31-40 >> $M
cat $K | cut -c 41-50 >> $M
cat $K | cut -c 51-60 >> $M
cat $K | cut -c 61-70 >> $M
cat $K | cut -c 71-80 >> $M
cat $K | cut -c 81-90 >> $M
cat $K | cut -c 91-100 >> $M

with fold -w10 $K > $M

Last edited by Chubler_XL; 03-16-2012 at 01:40 AM..
# 3  
Old 03-16-2012
Thanks Chubler_XL that works nicely.

Now on to the logical OR comparisons. To this I have not clue as to where to start Smilie
# 4  
Old 03-18-2012
Of course there are much better ways to do this than in shell script. An awk/perl script could probably xor two hex values and get the answer directly.

But as a learning excersize in shell string manupliation, looping and logical operations its an intersting problem.

Using bash you could xor two binary strings like this:

Code:
#!/bin/bash
A="11101011000101101011"
B="00001111101110100000"
i=0
while [ $i -lt ${#A} ]
do
  [ "${A:i:1}" -eq 1 ] || [ "${B:i:1}" -eq 1 ] && C=${C}1 || C=${C}0
  let i=i+1
done
echo $C

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo multi-line string via heredoc syntax

$ cat bashtest #!/usr/local/bin/bash echo <<<"EOF" line1 line2 line3 EOF $ ./bashtest ./bashtest: line 3: line1: command not found ./bashtest: line 4: line2: command not found ./bashtest: line 5: line3: command not found ./bashtest: line 6: EOF: command not found What am i doing... (4 Replies)
Discussion started by: urello
4 Replies

2. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

3. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

4. Shell Programming and Scripting

Multi line document to single lines based on occurance of string

Hi Guys, I am new to awk and sed, i am working multiline document, i want to make make that document into SINGLE lines based on occurace of string "dwh". here's the sample of my problem.. dwh123 2563 4562 4236 1236 78956 12394 4552 dwh192 2656 46536 231326 65652 6565 23262 16625623... (5 Replies)
Discussion started by: victor369
5 Replies

5. Shell Programming and Scripting

Parse file from 2nd line in shell script

Hi, I need to parse input file from 2nd line. Input file contents are, ABC123;20100913115432;2000000;NO; 04;AAA;09;DDD;601020304;AAAA;1;OPTA1;OPTA2;;; 04;BBB;09;BBB;601020304;BBBB;0;OPTB1;OPTB2;OPTB3;OPTB4;OPTB5; 04;CCC;09;DDD;601020304;CCCC;1;;;;; For each line, 1] I need to check... (17 Replies)
Discussion started by: Poonamol
17 Replies

6. Shell Programming and Scripting

Parse Multi-Section Configuration File

Hello all, Sample configuration file: username = root password = admin IpAddress = 192.168.2.90 HttpCommand = /getfile?hello.jpg Username = root2 Password = admin2 Passive = no Host = 192.168.1.100 Path = /uploads Username = root3 Password = adming Passive = no Host =... (10 Replies)
Discussion started by: LAVco
10 Replies

7. Shell Programming and Scripting

Search string and return only the 5th line

I have looked at almost every posting on here regarding, grep, sed, awk, nawk, perl methods to perform this function, but they are just not returning the results I hope to see. Example File: abcdef 123456 ghijkl 7890123 7890123 7890123 7890123 mnopqrs 456789 This is the most... (4 Replies)
Discussion started by: doza22
4 Replies

8. Shell Programming and Scripting

Parse a string in XML file using shell script

Hi! I'm just new here and don't know much about shell scripting. I just want to ask for help in creating a shell script that will parse a string or value of the status in the xml file. Please sample xml file below. Can you please help me create a simple script to get the value of status? Also it... (46 Replies)
Discussion started by: ayhanne
46 Replies

9. Shell Programming and Scripting

return line below string grepp'ed for

Hi there Does anybody know how to get grep to return the line underneath the string i am grepping for ie, if i have this file hello everybody how are you and i issue #cat file | grep how I would like it to return the line below (in this case the string "are") Is this... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

10. Shell Programming and Scripting

Perl: Search for string then parse next line

Hi All, I have a file that I need to be able to find a pattern match on one line then parse data on the next or subsequent lines - I will know which line needs to be parsed beforehand. This is what I currently have: while (<COMMAND_OUT>) { if ($_ =~ m/TEST/) { ... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question