to extract specific values twice in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting to extract specific values twice in a file
# 1  
Old 08-14-2009
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
# 2  
Old 08-14-2009
Quote:
Originally Posted by techmoris
...
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
...
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
# 3  
Old 08-14-2009
Or if you use perl, just use the split.

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

# 4  
Old 08-14-2009
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 [code] and [/code] 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


Code:
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 Smilie
# 5  
Old 08-15-2009
if its hard to follow split you can follow this..
Code:
awk -F"[,.]" '{print $3","$3"."$4}' filename

# 6  
Old 08-17-2009
Code:
echo "xyz.txt,12345.xml" | sed 's/[^,]*,\([0-9][0-9]*\)/\1,\1/'

# 7  
Old 08-17-2009
Quote:
Originally Posted by summer_cherry
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to match file1 and extract specific tag values

File2 is tab-delimeted and I am trying to use $2 in file1 (space delimeted) as a search term in file2. If it is found then the AF= in and the FDP= values from file2 are extracted and printed next to the file1 line. I commented the awk before I added the lines in bold the current output resulted. I... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Get specific values from a file, help

Dear all, I have a specific problem that i cannot solve and I hope someone here can help me. :) I have two text files with one column of values. Example: File1: 67 94 95 . . File2 0.1 0.003 0.5 . . (3 Replies)
Discussion started by: Higgo
3 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

Extract values from a specific column to the end

Hello friends, I have a text file with many columns (no. columns vary from row to row) separated by space. I need to collect all the values from 18th column to the end from each line and group them as pairs and then numbering like below.. 1. 18th-col-value 19th-col-value 2. 20th-col-value ... (5 Replies)
Discussion started by: prvnrk
5 Replies

5. Shell Programming and Scripting

Extract a specific string from a file

Hi, I have a file whose contents are as follows. 2013-03-08/15:09:20.134 INFO 00000000-00000000 0034 09700400 CON_IN SessionID:ED5E1400-4805-85E2-17B2-5BE45684886A Connection ID:ED5E1400-4805-68F1-BB1D-F06496BCF910 TO:<sip:51234999@10.239.94.146:5060 FROM:<sip:9302280716@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies

6. Shell Programming and Scripting

Extract values from an XML File

Hi, I need to capture all the attributes with delete next to it. The source XML file is attached. The output should contain something like this below: Attributes = legacyExchangeDN Action = Delete Username = Hero Joker Loginid = joker09 OU =... (4 Replies)
Discussion started by: prvnrk
4 Replies

7. Shell Programming and Scripting

Extract specific lines from a file

Hi, I have a file which contains DDL statements- CREATE TABLE, CREATE INDEX, ALTER TABLE etc. I have to only pick CREATE TABLE statements from the file- Source : ---------------------------------------------- --DDL for table abc -------------------------------------------- CREATE TABLE... (4 Replies)
Discussion started by: newb
4 Replies

8. UNIX for Dummies Questions & Answers

Extract Unique Values from file

Hello all, I have a file with following sample data 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26... (5 Replies)
Discussion started by: simonsimon
5 Replies

9. Shell Programming and Scripting

to extract specific value 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 like 12345 04567 12134 Please advice!! (3 Replies)
Discussion started by: techmoris
3 Replies

10. Shell Programming and Scripting

Extract values from log file

I would like to write a shell script that will parse through a file similar to the sample below. The data in the file is redirected from rsync into a log file. I would like to call a shell script to parse through and pick out the number beside the percent sign inside the parentheses in the last... (5 Replies)
Discussion started by: wdympcf
5 Replies
Login or Register to Ask a Question