1.

Solve : Extract data rows (Tough One)?

Answer»
Can you all help me about this one below...

There is a txt file CONTAIN data. I want to extract every 2 row of data out of all the data in the txt file.

$cat file.txt ... then the output

ERROR : No input. (error description for account below)
ERROR : AccNo=1111 ServiceID=1
ERROR : SERVICE error. (error description for account below)
ERROR : AccNo=2222 ServiceID=1
ERROR : Network error. (error description for account below)
ERROR : AccNo=3333 ServiceID=1
..... (all with the same format)

How can i select only the AccNo and the error desc.
The output i desire is

AccNo=1111 No input
AccNo=2222 Service Error


I cannot USE grep cause it will take all the data in the row. How can is select row
I try to seperate those data like below

file1.txt file2.txt
AccNo=1111 No input
AccNo=2222 Service Error

But, how can i append those file so it can be in 1 row.

AccNo=1111 No input
AccNo=2222 Service Error


Please assist me coz i really need help..thankssed -n 'p;n' file.txtfor the odd lines, and
sed -n 'n;p' file.txtfor the even lines.

I suspect you can work out the rest. Not sure of any easy way to combine the output of the 2 files, so I'd program it in awk with a couple of tr and sed tweaks to simplify the awk program.

Code: [Select]cat file.txt | tr '()' '::' | sed -e 's/ServiceID=1//' | awk -F: '($3 == "error description for account below"){error = $2}($3 != "error description for account below"){print $2 error}'
The tr changes the brackets to colons, so awk can use colon as a field delimeter.

The sed removes the ServiceID only if its 1. You can adapt the sed command to encompass other things later.

The awk program is pretty dumb by only using a unequal test, but you have the basis of the program, so I'm sure you can adapt it to properly check that the second field contains AccNo.

I get the feeling that I might be doing someone's homework for them, so I'm in future I'll just refer any questions back to this forum for examples of how to grep awk sed tail and tr. I'm pretty sure there's enough here now for anyone to work out how to do most things.Quote
Code: [Select]cat file.txt | tr '()' '::'
above can be written
Code: [Select]tr '()' '::' < file.txt
Useless use of cathmm..thanks for all your replies. It really help me a lot.

But, i still dont know how to append those words into a row.

ERROR : No input. (error description for account below)
ERROR : AccNo=1111 ServiceID=1
ERROR : Service error. (error description for account below)
ERROR : AccNo=2222 ServiceID=1


the problem is, i want to append those words into a row so i can put in the database.

ERROR : No input. AccNo=111
ERROR : Service error. AccNo=2222

i tried to use
cat file.txt > file2.txt

but it just append those word into another row. Can i put the account no in another column of a row? Or is it not possible because of the error description is not fixed and can be different for each account. So, it is not possible for me to use `cut` cmd. how can i do itQuote
hmm..thanks for all your replies. It really help me a lot.

But, i still dont know how to append those words into a row.

ERROR : No input. (error description for account below)
ERROR : AccNo=1111 ServiceID=1
ERROR : Service error. (error description for account below)
ERROR : AccNo=2222 ServiceID=1


the problem is, i want to append those words into a row so i can put in the database.

ERROR : No input. AccNo=111
ERROR : Service error. AccNo=2222

i tried to use
cat file.txt > file2.txt

but it just append those word into another row. Can i put the account no in another column of a row? Or is it not possible because of the error description is not fixed and can be different for each account. So, it is not possible for me to use `cut` cmd. how can i do it

Not the best solution, but possible one.
Code: [Select]sed -n 'p;n' file.txt > 1
sed -n 'n;p' file.txt > 2
sed -i 's/ERROR : \(AccNo.*\) Service.*/\1/' 1
sed -i 's/ERROR : \(.*\) (error.*/\1/' 2
paste 2 1 > new.txt
Quote
Useless use of cat
Well spotted ghostdog74

Quote
Code: [Select]paste 2 1 > new.txt
Thanks ghostdog74 for introducing me to a new command. I've been using Unix for 20 YEARS, and not come accross paste before.

I still prefer awk programming in this SCENARIO, as its more flexible.
I hope you don't mind me quoting this from your PM, as its something other awk novices might be interested in ...

Quote
Quote
awk -F: '($3 == "error description for account below"){error = $2}($3 != "error description for account below"){print $2 error}'
for $3 and error ... is it a variable u declare in the awk cmd or is it referring to the column of the data.

"error" is an undeclared variable which I made up, and $3 means the third field from the input file (where as I said before -F: defines colon as the field seperator).

Remember in awk scripts not to prefix variable names with dollar, as you would in shell scripting. Here is an example of what would happen.
Code: [Select]echo one two three four | awk 'BEGIN{fred=3}{print $fred}'This does not display "3" as you might think, but in fact displays the third field "three" instead.

Finally, here's an example of a predeclared variable in awk
Code: [Select]ls *.txt | awk '{print "mv " quote $0 quote " " quote $0 ".old" quote}' quote='"' | shHere the $0 means the entire line


Discussion

No Comment Found