Assign regular expression change to only first two columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign regular expression change to only first two columns
# 1  
Old 12-19-2010
Assign regular expression change to only first two columns

input
Code:
1_2  2_3  4_4  4_5
2_2  4_5  4_4  5_5

output
Code:
1  2  2  3  4_4  4_5
2  2  4  5  4_4  5_5

I used the following command but it changes every column. i just want to change first two.

Code:
tr '_' '\t'

# 2  
Old 12-20-2010
Quote:
Originally Posted by quincyjones
I used the following command but it changes every column. i just want to change first two.
Code:
awk '{sub(/_/,FS,$1);sub(/_/,FS,$2)}1' file

This User Gave Thanks to danmero For This Post:
# 3  
Old 12-20-2010
Code:
sed 's/_/ /;s/_/ /' file

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 12-20-2010
Quote:
Originally Posted by Scrutinizer
Code:
sed 's/_/ /;s/_/ /' file

Your solution apply for the first two occurrences not
Quote:
Originally Posted by quincyjones
Assign regular expression change to only first two columns
# 5  
Old 12-20-2010
you can try this also -
Code:
cat <file> | sed 's/_/ /1'| sed 's/_/ /1'


Last edited by Scott; 12-20-2010 at 07:56 AM.. Reason: Code tags
# 6  
Old 12-20-2010
Quote:
Originally Posted by danmero
Code:
awk '{sub(/_/,FS,$1);sub(/_/,FS,$2)}1' file

If any "column" has more than one occurrence then your solution would fail also, given the expected output. The FS is probably useful, though Smilie

Quote:
Originally Posted by adc22
you can try this also -
Code:
cat <file> | sed 's/_/ /1'| sed 's/_/ /1'

No need for cat, and using 1 is the same as not using g, which is the same as Scrutinizer already posted.
# 7  
Old 12-20-2010
Quote:
Originally Posted by scottn
If any "column" has more than one occurrence then your solution would fail also, given the expected output.
That's correct , let's try
Code:
awk '{gsub(/_/,FS,$1);gsub(/_/,FS,$2)}1' file


This following two solution will fail if first OR second OR both target column's don have the special char OR the target column's have more that one occurrence of special char.
Code:
sed 's/_/ /1'| sed 's/_/ /1' file

Code:
sed 's/_/ /;s/_/ /' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign result of boolean expression?

Hello I would to write the test on one line like : declare -i x=0 y=0 ........ some code assign value 0 or 1 to x and y ........ # if either x or y or both is set to 1, then do something if -o ; then do_something fi Any help is welcome (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Assign expression to a variable

This code strips out any . It works great echo "127001" | tr -d "" I would like to do the same thing but with shell scripting. User would enter: ./test 127001 Output should be: 127.0.0.1 I would like to assign it to a different variable. I have something like this but I get a syntax error and... (7 Replies)
Discussion started by: Loc
7 Replies

3. Fedora

Use of regular expression

Hi, I need some help. My task is, to write a "one-line" command, which must use ls and awk. Task: Write a command-line, which should rename all files in dir from form "value1.dok" to "value2.doc". And value2=value1+1. For example: ls | awk -F: '{print "mv "$0" "$1+1".doc"}' | sh But... (3 Replies)
Discussion started by: John_Light
3 Replies

4. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

5. Programming

How to use regular expression in C/C++ ?

how to code with regexp.h some one can give me instance? thx (4 Replies)
Discussion started by: AKB48
4 Replies

6. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

7. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

8. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

9. Programming

What does the regular expression ['(^[^~]+~).*'] mean?

What does the regular expression +~).*'] mean while using it with regexec.When the string "RCHNUSNT35C~rs07/ASM-RS07" is used with the regular expression +~).*'] regexec gives an error. I know what regexec does,but i do not understand what this expression means wrt to this string... any help... (2 Replies)
Discussion started by: anupamar
2 Replies

10. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question