0

Below is a input.

!{ID=34, ID2=35} > !{ID=99, ID2=23} > !{ID=18, ID2=87} < 

I am trying to make a final result like as following. That is, wanted to remove space,'{' and '}' character and check if the next line is '>' or '<'. In fact, the input above is repeated. I also need to parse '>' and '<' character so I will put the parsed string(YES or NO) into database.

ID=34,ID=35#YES#NO ID=99,ID=23#YES#NO ID=18,ID=87#NO#YES 

So, with 'sub' function I thought I can replace the space with blank but the result shows:

1#YES#NO

Can you let me know what is wrong? If possible, teach me how to remove '{' and '}' as well. Appreciated if you could show me the awk file version instead of one-liner.

BEGIN { VALUES = "" L_EXIST = "NO" R_EXIST = "NO" } /!/ { VALUES = gsub(" ", "", $0); getline; if ($1 == ">") L_EXIST = "YES"; else if ($1 == "<") R_EXIST = "YES"; print VALUES"#"L_EXIST"#"R_EXIST } END { } 
4
  • 1
    You need to explain more clearly how the input maps to the output. Some more examples would perhaps be helpful. Commented Nov 13, 2015 at 14:15
  • echo '!{ID=34, ID2=35}' | tr -d '[!{ }]' returns ID=34,ID2=35 (-d to delete and then a set of characters). Not sure what the extra #YES#NO means. Commented Nov 13, 2015 at 14:24
  • Thanks. I need to use only awk though. Commented Nov 13, 2015 at 14:27
  • Those single quotes around '>' are new - are they supposed to be there? What would the corresponding output be for those lines of input? Commented Nov 13, 2015 at 15:11

2 Answers 2

4

Given your sample input:

$ cat file !{ID=34, ID2=35} > !{ID=99, ID2=23} > !{ID=18, ID2=87} < 

This script produces the desired output:

BEGIN { FS="[}{=, ]+"; RS="!" } NR > 1 { printf "ID=%d,ID=%d#%s\n", $3, $5, ($6==">"?"YES#NO":"NO#YES") } 

The Field Separator is set to consume the spaces and other characters between the parts of the line that you're interested in. The Record Separator is set to !, so that each pair of lines is treated as a single record.

The first record is empty (the start of the first line, up to the first !), so we only process the ones after that. The output is constructed using printf, with a ternary to determine the last part (I assume that there are only two options, > or <).

Sign up to request clarification or add additional context in comments.

3 Comments

If I have one more question which is very similar to this one, what would be a good approach? New question or update Question? Sorry, I haven't this site a lot.
@Sigularity I've just updated to simplify it a bit. I would recommend that you try to understand the code here and hopefully you will be able to adapt it to your slightly different scenario. If not, then perhaps it would be best to ask a new question. If you do decide to ask a new question, don't forget the advice we've given you about how to ask.
Sure. I will keep it out. Thank you so much.
1

Let's say you have this input:

input.txt

!{ID=34, ID2=35} !{ID=36, ID2=37} > 

You can use the following awk command

awk -F'[!{}, ]' 'NR>1{yn="NO";if($1==">")yn="YES";print l"#"yn}{l=$3","$5}' input.txt 

to produce this output:

ID=34,ID2=35#NO ID=36,ID2=37#YES 

5 Comments

Thank you. The input is always like !{ID=34, ID2=35} and '>' then !{ID=36, ID2=37} and '>'. Can you show me how to transform your solution into awk file?
What's the meaning of YES#NO then?
So it is sometimes > and sometimes < ?
@Sigularity if you have additional detail to add, please edit your question to make it more clear.
@Sigularity ??? Can you give a self-contained example of your input and the expected output? (Please edit the question, don't post it in comments!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.