LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   data from input file to be taken and send to output file (https://www.linuxquestions.org/questions/linux-newbie-8/data-from-input-file-to-be-taken-and-send-to-output-file-821887/)

grishu 07-24-2010 09:39 AM

data from input file to be taken and send to output file
 
Hi, I am new to shell scripting.What i am trying is to write a shell script which take the input file and output should like as mentioned below.
Output file should have data till SOK (marked in red)from every second line and then the selected data(marked in green) from 4th line.
So selected data from 2nd and 4th line in one line of O/P file and then similarly selected data from 6th and 8th line in second line of O/P file.

Input File:

3c3
< c1111;11.11.11.11;pOK;SOK:abcde;Universe:aa
---
> cz22222;11.11.11.11;pERROR;SERROR;Universe:aa
44c44
< IE3333;11.11.11.11;pOK;SOK:bbbbb;Universe:bb
---
> IE4444;11.11.11.11;pOK;SERROR;Universe:dd

Output File should look like:
c1111;11.11.11.11;pOK;SOK;pERROR;SERROR
IE3333;11.11.11.11;pOK;SOK;pOK;SERROR

regards,
g

jkirchner 07-24-2010 10:44 AM

Is this homework? If so, you should be doing it on your own....

jschiwal 07-24-2010 11:19 AM

I didn't think this was homework, and prepared a solution as a challenge. Now I have second thoughts.

Could you explain what this data represents? I'll provide a hint that I used the sed command. Also when you have a multi-line pattern, you usually need to use the hold register. Entering parts of the pattern in Google, the results were either chinese restaurants or test subject monkeys making strange sounds.

If this is a homework question, show what you have tried, and we then will will be able to provide more hints.

TB0ne 07-24-2010 11:36 AM

Quote:

Originally Posted by grishu (Post 4043955)
Hi, I am new to shell scripting.What i am trying is to write a shell script which take the input file and output should like as mentioned below.
Output file should have data till SOK (marked in red)from every second line and then the selected data(marked in green) from 4th line.
So selected data from 2nd and 4th line in one line of O/P file and then similarly selected data from 6th and 8th line in second line of O/P file.

Input File:

3c3
< c1111;11.11.11.11;pOK;SOK:abcde;Universe:aa
---
> cz22222;11.11.11.11;pERROR;SERROR;Universe:aa
44c44
< IE3333;11.11.11.11;pOK;SOK:bbbbb;Universe:bb
---
> IE4444;11.11.11.11;pOK;SERROR;Universe:dd

Output File should look like:
c1111;11.11.11.11;pOK;SOK;pERROR;SERROR
IE3333;11.11.11.11;pOK;SOK;pOK;SERROR

regards,
g

I echo the sentiments of jschiwal...show us what you've tried. We will all be glad to HELP you with this, but we're not going to write it for you. There are thousands of online shell scripting tutorials you can find via Google.

And this does sound like homework.....

grishu 07-24-2010 11:38 AM

Hey Guys, this is not an homework :).The input file is a result of corba script which runs on one of the applications of telecom network.

TB0ne 07-24-2010 03:52 PM

Quote:

Originally Posted by grishu (Post 4044027)
Hey Guys, this is not an homework :).The input file is a result of corba script which runs on one of the applications of telecom network.

Ok, great....now again show us what you've written/done, and where you're getting stuck. Again, we're not going to write it for you, but will be glad to help you with it.

grishu 07-28-2010 04:59 PM

haha, i just saw the last post. Tomorrow i will spare some 15-20 min and i will finish this off. I will post you then :)

grishu 07-29-2010 06:06 AM

Here comes the much awaited script you all were looking for :).


rm rout
i=1
sed -n 'n;p' $1 | while read line
do
if [[ $i -eq 1 ]]
then
L1=`echo $line | cut -d';' -f1,2,3,4`
i=0
continue
else
L2=`echo $line | cut -d';' -f3,4`
i=1
fi
echo $L1";"$L2 >> rout
done

grail 07-29-2010 08:00 AM

So it seems you have yet to learn about [ code ][ /code ] tags (without the spaces), but as for your code, it seems to get what you want and also a little
extra as one of your fields (number 4) has a colon and not a semi-colon. So here is something that looks ok:
Code:

awk 'BEGIN{FS="[:;]";OFS=";"}/^[<>]/{if(!a){a = gensub($5".*","","g")}else{print a,$3,$4;a=""}}' file

ghostdog74 07-29-2010 08:41 AM

Code:

#!/bin/bash

while read -r line
do
  IFS=";"
  set -- $line
  case "$1" in
    "<"*)
      printf "${1/< /};$2;$3;${4%:*};"
      ;;
    ">"*)
      echo "$3;${4%:*}"
  esac
done <"file"


grishu 08-02-2010 04:03 PM

Hey,thanks but when i tried with

awk 'BEGIN{FS="[:;]";OFS=";"}/^[<>]/{if(!a){a = gensub($5".*","","g")}else{print a,$3,$4;a=""}}' file

I am getting
awk: syntax error near line 1
awk: illegal statement near line 1

Please advise.

grishu 08-02-2010 04:32 PM

Hey, with this code...

---------
#!/bin/bash

while read -r line
do
IFS=";"
set -- $line
case "$1" in
"<"*)
printf "${1/< /};$2;$3;${4%:*};"
;;
">"*)
echo "$3;${4%:*}"
esac
done <"file"

I cannot see any output.Also i want to get rid of arrow and the space marked in red from the output which i am getting from the code i wrote. Assist.
< c1111;11.11.11.11;pOK;SOK:abcde;Universe:aa
---
> cz22222;11.11.11.11;pERROR;SERROR;Universe:aa

grail 08-03-2010 01:57 AM

You are using your data stored in the file name 'file'?

I used it on the data you provided and received the following output:
Code:

< c1111;11.11.11.11;pOK;SOK:;pERROR;SERROR
< IE3333;11.11.11.11;pOK;SOK:;pOK;SERROR


flores81 08-03-2010 02:35 AM

does that work?

Wim Sturkenboom 08-03-2010 02:44 AM

Off topic
 
A little of topic
Quote:

Originally Posted by grail (Post 4049007)
So it seems you have yet to learn about [ code ][ /code ] tags (without the spaces)

And you can learn about the noparse tag ;)
[code]

[/code]

Somebody pointed this out to me about a year ago or so :)


All times are GMT -5. The time now is 02:28 AM.