to extract specific values twice in a file

Hi Friends,

I have a file with the following values..

xyz.txt,12345.xml
abc.txt,04567.xml
cde.txt,12134.xml

I would like to extract all the 2nd column values twice as shown in the example like
12345,12345.xml
04567,04567.xml
12134,12134.xml

Please advice!!

In the formus one of our friends adviced me in fetching the 2nd column values

awk -F'[.|,]' '{print $3}' oldfile > newfile

Well, that awk script answers your question for the most part.

Play around a bit with that script (it doesn't bite), and you'll find the solution.

For example, check the token values - $1, $2, etc. Also see if you can print the same token value twice.

tyler_durden

Or if you use perl, just use the split.

split ($col1,$col2) ",", $line;

To keep the forums high quality for all users, please take the time to format your posts correctly.

  1. 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 and by hand.)
  2. 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.
  3. 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
Reply With Quote

awk -F, '{split($NF,a,".");print a[1]FS$NF}' file

If you don't try to solve the problems yourself you'll never learn :rolleyes:

if its hard to follow split you can follow this..

awk -F"[,.]" '{print $3","$3"."$4}' filename
echo "xyz.txt,12345.xml" | sed 's/[^,]*,\([0-9][0-9]*\)/\1,\1/'

What is \1 used for , Just want to know, I am new to sed

The parentheses capture a substring in the matching expression. If there were more than 1 set, then other escapes could return those fields.

    s/regexp/replacement/
          Attempt to match regexp against the pattern space.  If successful, replace that portion matched with  replacement.   The  replacement may  contain  the special character & to refer to that portion of the pattern space which matched, and the special escapes \\1 through \\9 to refer to the corresponding matching sub-expressions in the regexp.