how to convert this file output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to convert this file output
# 1  
Old 10-19-2009
how to convert this file output

hi, How do I do this with sed and egrep? is that even possible?


I have the following file with this content

Code:
 
#OQ#||1|XDzb34_zcSETA|SETA
#OQ#||3|XDzc24_zcSETA|SETA
#OQ#||4|XDze12_zcSETA|SETA
#OQ#||6|XDzf35_zcSETA|SETA
#OQ#||7|ADzf38_zc|SETA

1. I want to strip out X and SETA in the 4th column if it contain X in the begin and SETA in the end.

Code:
 
#OQ#||1|Dzb34_zc|SETA
#OQ#||3|Dzc24_zk|SETA
#OQ#||4|Dze12_zf|SETA
#OQ#||6|Dzf35_ze|SETA
#OQ#||7|ADzf38_zc|SETA


2. If the 1st step is true, then I want to strip out underscore second part ,

so the final output file should be:

Code:
 
#OQ#||1|Dzb34|SETA
#OQ#||3|Dzc24|SETA
#OQ#||4|Dze12|SETA
#OQ#||6|Dzf35|SETA
#OQ#||7|ADzf38_zc|SETA


Thanks

Last edited by zaxxon; 10-20-2009 at 03:29 AM.. Reason: C O D E tags
# 2  
Old 10-20-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

******************************************************

Code:
$> cat infile
#OQ#||1|XDzb34_zcSETA|SETA
#OQ#||3|XDzc24_zcSETA|SETA
#OQ#||4|XDze12_zcSETA|SETA
#OQ#||6|XDzf35_zcSETA|SETA
#OQ#||7|ADzf38_zc|SETA
$> sed -e '/|X.*SETA|.*/ { s/|X/|/g; s/SETA|/|/g; s/_//g}' infile
#OQ#||1|Dzb34zc|SETA
#OQ#||3|Dzc24zc|SETA
#OQ#||4|Dze12zc|SETA
#OQ#||6|Dzf35zc|SETA
#OQ#||7|ADzf38_zc|SETA

# 3  
Old 10-20-2009
Thanks. But I got a syntax error.
also, your result is slightly different from my original post. your code doesn't remove "text" after the underscore. I need to remove "_" and "text after underscore" if X*SETA exists.



Is it because my sed version is too low for your complicated code?

'X.*SETA' is not recognized as an internal or external command,
operable program or batch file.

when I run it in bash mode, I got error: sed: garbage after command

Note: I used MKS tool in window server. it's unix tool which can run on window.

Last edited by jbchen; 10-20-2009 at 02:54 PM..
# 4  
Old 10-20-2009
Code:
sed 's/|X\(.*\)_.*SETA|/|\1|/' infile

# 5  
Old 10-20-2009
bash
Code:
while IFS="|" read -r a b c d e
do    
    case "$d"  in
        X*SETA )
            d=${d/#X/}
            d=${d/%_*SETA/}
        ;;
    esac
    echo "$a|$b|$c|$d|$e"
done < "file"

# 6  
Old 10-21-2009
You are right - I didn't see that it is needed to delete all characters behind an underscore if there is one up to the pipe.
The problem you got with your sed could be since it is a different version of sed. I am using GNU sed (gsed) which is more comfortable. Other versions of sed have problems with {} in one line - they want their contents etc. in separate lines or by using a ^J which is a bit awkward to write/read.

Nevertheless here you go:

Code:
$> cat infile
#OQ#||1|XDzb34_zcSETA|SETA
#OQ#||3|XDzc24_zcSETA|SETA
#OQ#||4|XDze12_zcSETA|SETA
#OQ#||6|XDzf35_zcSETA|SETA
#OQ#||7|ADzf38_zc|SETA
$> sed 's/|X/|/g; s/SETA|/|/g; s/_[^|]*//' infile
#OQ#||1|Dzb34|SETA
#OQ#||3|Dzc24|SETA
#OQ#||4|Dze12|SETA
#OQ#||6|Dzf35|SETA
#OQ#||7|ADzf38|SETA

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert shell script output txt file to html table

My concnern related to the post -Convert shell script output txt file to html table, in this how to print the heading as color. awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' <filename> (8 Replies)
Discussion started by: sarajobmai
8 Replies

2. Shell Programming and Scripting

Need to convert output.txt into html file

I have output.txt file generated through shell scripts which need convert in tabular format using html can you please help me output.txt Token State Date1 Date2 Description Name 34567 open 27/06/13 28/06/13 ... (5 Replies)
Discussion started by: vijay_rajni
5 Replies

3. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

4. Shell Programming and Scripting

Convert TZ output to a different format

Friends, I am trying to convert my local server timezone EST to UTC and for which I used the TZ command, see below $ date Thu Dec 6 10:14:14 EST 2012 $ $ TZ=UTC date -d '10:14 EST' Thu Dec 6 15:14:00 UTC 2012 Now I would like to have the same output in 'yyyymmdd hh:mm' format. ... (4 Replies)
Discussion started by: vivek_damodaran
4 Replies

5. Shell Programming and Scripting

convert the output in table format

Hi All, I have a output like below values val1=test.com val2=10.26.208.11 val3=en1 val4=test-priv1.com val5=192.168.3.4 val6=en2 val7=test-priv2.com val8=192.168.4.4 val9=en3 val10=test-vip.com val11=10.26.208.9 val12=$val3 I want to convet this output values into below... (1 Reply)
Discussion started by: kamauv234
1 Replies

6. Shell Programming and Scripting

Convert shell script output txt file to html table

Hi, I have script which generates the output as below: Jobname Date Time Status abc 12/9/11 17:00 Completed xyz 13/9/11 21:00 Running I have the output as a text file. I need to convert it into a HTML Table and sent it thru email ... (6 Replies)
Discussion started by: a12ka4
6 Replies

7. Shell Programming and Scripting

Convert sql output to csv file via bash tools

hi Can anybody help me with converting such structure into csv file for windows : BAT_ID ID_num CVS_LINE A_SEG SKILL_TO A_CUSTOMER_TYPE --------- ---------- --------------------------------- ---------- ------------------ ----------- 14-MAY-11 777752 ... (4 Replies)
Discussion started by: kvok
4 Replies

8. Shell Programming and Scripting

AWK Script to convert input file(s) to output file

Hi All, I am hoping someone can help me with some scripting I need to complete using AWK. I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format. The Input file is:- aaaa bbbbb ccccc 1 xxxx aaa bbb aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Discussion started by: jason_v_brown
9 Replies

9. Shell Programming and Scripting

convert timezone into human output

Hi I have a command which returns a timezone, Ej: root@bsades2: /usr/local/bin # lsuser -a time_last_login israel israel time_last_login=1279032223 Question: I want to parse this timezone '1279032223' into a 'martes, 13 de julio de 2010 16:43:43' from the ksh shell. Is itt possible? ... (3 Replies)
Discussion started by: iga3725
3 Replies

10. Shell Programming and Scripting

Convert output to string

Hi Guys, is there any command to convert the output returned by the below command to string format: Code: sed 1!d filename Output is : 108 ---------- Post updated at 11:03 AM ---------- Previous update was at 11:00 AM ---------- Because i am using this output as string parameter ... (4 Replies)
Discussion started by: kam786sim
4 Replies
Login or Register to Ask a Question