LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-24-2010, 09:39 AM   #1
grishu
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Rep: Reputation: 0
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
 
Old 07-24-2010, 10:44 AM   #2
jkirchner
Member
 
Registered: Apr 2007
Location: West Virginia
Distribution: Pop!_OS
Posts: 945

Rep: Reputation: 297Reputation: 297Reputation: 297
Is this homework? If so, you should be doing it on your own....
 
Old 07-24-2010, 11:19 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 07-24-2010 at 11:29 AM. Reason: second thoughts on homework issue.
 
Old 07-24-2010, 11:36 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by grishu View Post
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.....
 
Old 07-24-2010, 11:38 AM   #5
grishu
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 07-24-2010, 03:52 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by grishu View Post
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.
 
Old 07-28-2010, 04:59 PM   #7
grishu
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 07-29-2010, 06:06 AM   #8
grishu
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 07-29-2010, 08:00 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 07-29-2010, 08:41 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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"
 
Old 08-02-2010, 04:03 PM   #11
grishu
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 08-02-2010, 04:32 PM   #12
grishu
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 08-03-2010, 01:57 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 08-03-2010, 02:35 AM   #14
flores81
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Rep: Reputation: 0
does that work?
 
Old 08-03-2010, 02:44 AM   #15
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Off topic

A little of topic
Quote:
Originally Posted by grail View Post
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
 
  


Reply



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
NFS Large File Copies Fail - Error writing to file: Input/output error deck- Linux - Server 10 08-01-2022 02:30 AM
Using command output as file input FlowState Linux - Software 1 05-14-2008 07:30 PM
Can't rm file with Input/Output error drlaz Linux - General 0 02-23-2005 06:06 PM
how to use the output of a file for input of a command sneak Linux - General 2 05-12-2004 09:21 AM
logging all keyboard input/output to a file div Linux - General 0 02-20-2001 05:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:42 PM.

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