Editing strings within lines of file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Editing strings within lines of file
# 1  
Old 03-18-2012
CPU & Memory Editing strings within lines of file

Dear All,

I have a file which contains a column with age, which is represented in two following patterns

1. "007/A" or ''007/a" or ''7 /a" ..... In this case A or a means year and I would like to extract only the numeric values eg 7 in the above case if this pattern exits in a line of file.

2. "004/M" or "004/m" where M or m means month ...... for these lines I would like to first extract the numeric value of Month eg. 4 in this case and then convert it into a value of years which would be 0.33 eg 4 divided by 12.

Can someone please enlighten on this problem?

Appreciate your feedback.

Have a nice Sunday.

Cheers

Pawan
# 2  
Old 03-18-2012
Please provide a sample input and desired output.
# 3  
Old 03-18-2012
OK ... the input for example is :
Code:
"029/A" 
 "029/a" 
 "29 /A" 
 "029/M" 
 "029/m"

and the desired output should be
Code:
29
29
29
2.4
2.4

Thank you
# 4  
Old 03-18-2012
Code:
perl -lne 'if(/(\d+)[ ]?\/a/i){$x=$1;$x=~s/^0*//;print"$x"}
if(/(\d+)[ ]?\/m/i){$y=$1;$y=~s/^0*//;printf "%0.1f\n",$y/12}' inputfile

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 03-18-2012
AWK:
Code:
awk -F"/" '{gsub("\"","")}$2~"a|A"{printf ("%d\n",$1)}$2~"m|M"{printf ("%.1f\n",$1/12)}' file

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 03-18-2012
Code:
awk -F'/|"' '$3~"m|M"{printf "%.1f\n", $2/12;next} {print $2+0}' infile


Last edited by Scrutinizer; 03-18-2012 at 01:46 PM..
This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

2. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

3. Shell Programming and Scripting

Egrep strings on different lines in file

test.txt: appleboy orangeletter sweetdeal catracer conducivelot I want to only grep out lines that contain "appleboy" AND "sweetdeal". however, the closest thing to this that i can think of is this: cat test.txt | egrep "appleboy|sweetdeal" problem is this only searches for all... (9 Replies)
Discussion started by: SkySmart
9 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. Shell Programming and Scripting

awk how to search strings within a file from two different lines

Hi, i would really appreciate any help anyone can give with the following info. Thanks in advance. I need to run a search on a file that contains thousands of trades, each trade is added into the file in blocks of 25 lines. i know the search has to take place between a time stamp specified... (4 Replies)
Discussion started by: sp3arsy
4 Replies

6. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

7. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

8. Shell Programming and Scripting

Adding strings to lines in a file

Hi all, I have a positional text file that comes from some source application. Before it is processed by destination application I have to add some header (suffix) to every record(line) in the file. e.g. Actual File ............... AccountDetails AcNO Name Amount 1234 John 26578 5678... (3 Replies)
Discussion started by: sharath160
3 Replies

9. UNIX for Dummies Questions & Answers

Counting no of lines between two strings in a file

Hi all, I'm very very new to UNIX and AWK world.Please help me in finding a solution for my problem. I'm having a file like this ----------------------------------------------------------------- ~Version Information VERS. 2.0: CWLS log ASCII Standard -VERSION 2.0 WRAP. ... (4 Replies)
Discussion started by: santyshyam
4 Replies

10. Shell Programming and Scripting

Extracting the lines between 2 strings of a file

Hi, I have a sql file and i need to extract the table names used in the sql file using a unix script. If i can extract the lines between the keywords 'FROM' and 'WHERE' in the file, my job is done. can somebody tell me how to do this using a shell script. If u can just let me know, how to... (2 Replies)
Discussion started by: babloo
2 Replies
Login or Register to Ask a Question