Help with if else with additional condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with if else with additional condition
# 1  
Old 07-24-2013
Help with if else with additional condition

Input file:
Code:
3420023_3422482,3422486_3423070 46910
1795263_1798343 32681
1837399_1838886 1534
2148674_2149696,2149698_2149772 10203
3825382_3825555,3827296_3827900 198
1839890_1840294 72
.
.

Output file
Code:
3420023_3422482,3422486_3423070 3420023_3422482,3422486_3423070 46910
1795263 1798343 32681
1837399 1838886 1534
2148674_2149696,2149698_2149772 2148674_2149696,2149698_2149772 10203
3825382_3825555,3827296_3827900 3825382_3825555,3827296_3827900 198
1839890 1840294 72
.
.

If column 1 have "_", I would like to change/replace the "_" into "\t" delimiter;
If column 1 have "_" and "," at the same time, I would like the desired output file have same data contents in column 1 and column 2;

Below is the command I try:
Code:
awk -F"_" '{print $1"\t"$2"\t"}' input_file
awk '$1~/,/{print $1"\t"$1"\t"$2"\t"}' input_file

I feel like the command I try is not really efficient to generate desired output file.
Thanks for any advice.
# 2  
Old 07-24-2013
Try:
Code:
awk '$1~/_/{ if($1~/,/) print $1 "\t" $1 "\t" $2 ; else { gsub(/_/,"\t",$1); print $1 "\t" $2 }}'  file

or
Code:
awk '$1~/_/{ if($1~/,/) $1=$1 "\t" $1; else gsub("_", "\t", $1) }1' file


--
The first example leaves out all lines that do not contain "_" in the first field, the second example does not...

Last edited by Scrutinizer; 07-24-2013 at 03:39 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

2. UNIX for Dummies Questions & Answers

How to run additional shells

Hi, unix newbi here. Im currently on the bash shell, what are the commands to run additional shells? for example i want to run csh and then ksh? Thanks. BT. (2 Replies)
Discussion started by: BillThompson
2 Replies

3. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

4. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

5. Linux

Additional mirrors on centos

How can I add additional mirrors to my CENTOS distro, according to this page AdditionalResources/Repositories - CentOS Wiki there are few fedora project repositories I'd like to add any of them but I don't know how? Thank you in advance (0 Replies)
Discussion started by: c0mrade
0 Replies

6. UNIX for Dummies Questions & Answers

Mounting an additional HW now no CLI

I have a SUN V245 with Solaris 10 newly installed. I have 4 73Gb HDs in this server and thought I'd use one of the other 3 to load some applications but after issueing a mount it appears that I no longer have a CLI interpreter -- # mount -F ufs /dev/dsk/c1t2d0s0 /usr # df bash:... (1 Reply)
Discussion started by: billwade
1 Replies

7. AIX

Install additional fonts ?

Hi, I have two fonts installed on my AIX machine: FONT FILE GLYPH FONT ID NAME SIZE ENCODING ==== ============== ===== ========= 0 Erg22.iso1.snf 12x30 ISO8859-1 1 Erg11.iso1.snf 8x15 ISO8859-1 I want to install more, how to obtain available fonts list ? What filesets I must install ?... (0 Replies)
Discussion started by: vilius
0 Replies

8. UNIX for Advanced & Expert Users

Help with Additional Password Features

Hello: I have a customer who is requesting the following security features on a Solaris 8 system: 1. Password history for the three previous passwords. 2. User account lockout after 3 failed login attempts. Can anyone help provide me with a solution or direction for the above? (1 Reply)
Discussion started by: rambo15
1 Replies

9. Shell Programming and Scripting

Additional character output

Hi there, I am just wondering if somebody can help me find out why am I getting additional characters on my "echo" command to create a text file. Here's my unix script : #!/bin/ksh #============================================================ # Script Name : chk_ORDHD.ksh # Description... (2 Replies)
Discussion started by: negixx
2 Replies
Login or Register to Ask a Question