Extract text from file then write variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract text from file then write variable
# 1  
Old 08-29-2013
Extract text from file then write variable

I have a text file that has many lines, and for each line I need to extract different sections of text, then write several variables from the data.
I can split out the various sections by selecting their position on each line as the column sizes will never vary.
A sample of my text file is below

Code:
"A 16   7G12Cooper              Amber                4"
"A 16   9G12Patrech             Erin                 4"
"A 16  12B12Donovan             Brody                4"
"A 16  14G12Tanea               Semira               4"
"A 16  70 G9Fink                Jade                 4"
"A 16  72 B8Srini               Sandip               4"
"A 16 938G12Calvar              Ashley               4"

the variables and selection criteria i am extracting is here
Code:
file="Names.EXT"
while read line
do 
    echo $line
    genderage=${line:9:3} #Loc 9th character in the line and select the next 3 Char
    lastnam=${line:12:20} #loc 12th character in the line and select the next 20 char
    firstnam=${line:32:21} #loc 32nd character in the line and select the next 21
    echo "$lastnam;$firstnam;;$gend;$age;;" >> MM_file.txt 
done <"$file"

Now to my specific Question,
for the variable $genderage, I need to delete the first character ( either B or G) and write a new variable called $gend that is either M or F depending on the original letter.
the remaining number could be either a 1 or 2 digit number which I need to write to another veriable $age.

the other question is how to delete all the trailing spaces from the other 2 variables? this can be done either when selection them, or when writing them out to a new file.
$lastnam
$firstnam

TIA
Ken

Last edited by vbe; 08-29-2013 at 10:42 AM.. Reason: indent...
# 2  
Old 08-29-2013
Did you peruse your shell's man page? This works with recent shells:
Code:
while read line
  do    #echo $line
        gendage=${line:9:3} #Loc 9th character in the line and select the next 3 Char
        gendage=${gendage##* }
        [ "${gendage:0:1}" = "B" ] && gend="M" || gend="F"
        age=${gendage:1}   
        lastnam=${line:12:20} #loc 12th character in the line and select the next 20 char
        lastnam=${lastnam%% *} 
        firstnam=${line:32:21} #loc 32nd character in the line and select the next 21
        firstnam=${firstnam%% *}
        echo "$lastnam;$firstnam;;$gend;$age;;"
  done < file
Cooper;Amber;;F;12;;
Patrech;Erin;;F;12;;
Donovan;Brody;;M;12;;
Tanea;Semira;;F;12;;
Fink;Jade;;F;9;;
Srini;Sandip;;M;8;;
Calvar;Ashley;;F;12;;

This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-30-2013
Thanks Rudi Smilie
I have read so much over the last few days re this and did not even think to use the shell. Looking at awk, sed and such and probably making life hard at the same time!

when at home later i will update my script accordingly

Ken
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Extract data from XML file and write in CSV file

Hi friend i have input as following XML file <?xml version="1.0"?> <Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.02"> <BkToCstmrDbtCdtNtfctn> <GrpHdr><MsgId>LBP-RDJ-TE000000-130042430010001001</MsgId><CreDtTm>2013-01-04T03:21:30</CreDtTm></GrpHdr>... (3 Replies)
Discussion started by: mohan sharma
3 Replies

4. Shell Programming and Scripting

Extract specific text from variable and put it into array

Dear community, I have to do something too hard for me :rolleyes:. I hope you can help me. This is an output coming from Oracle query, stored in a file called query.out, there are many rows, but I read them, one by one, using while/read/done. Assuming each row is contained into $line variable... (8 Replies)
Discussion started by: Lord Spectre
8 Replies

5. Shell Programming and Scripting

how to write bash script that will automatically extract zip file

i'm trying to write a bash script that that will automatically extract zip files after the download. i writed this script #!/bin/bash wget -c https://github.com/RonGokhle/kernel-downloader/zipball/master CURRENDIR=/home/kernel-downloader cd $CURRENDIR rm $CURRENDIR/zipfiles 2>/dev/null ... (2 Replies)
Discussion started by: ron gokhle
2 Replies

6. Shell Programming and Scripting

Perl extract number from file & write to file

I have 1 file that has elements as follows. Also the CVR(10) and the word "SAUCE" only appear once in the file so maybe a grep command would work? file1 CVR( 9) = 0.385E+05, ! VEHICLE CVR(10) = 0.246E+05, ! SAUCE CVR(11) = 0.162E+03, ! VEHICLE I need to extract the... (6 Replies)
Discussion started by: austinj
6 Replies

7. Shell Programming and Scripting

How to write a script to extract strings from a file.

Hello fourm members, I want to write a script to extarct paticular strings from the all type of files(.sh files,logfiles,txtfiles) and redirect into a log file. example: I have to find the line below in the script and extract the uname and Pwds. sqsh -scia2007 -DD0011uw01 -uciadev... (5 Replies)
Discussion started by: rajkumar_g
5 Replies

8. Shell Programming and Scripting

Extract data from an XML file & write into a CSV file

Hi All, I am having an XML tag like: <detail sim_ser_no_1="898407109001000090" imsi_1="452070001000090"> <security>ADM1=????</security> <security>PIN1=????</security> <security>PIN2=????</security> ... (2 Replies)
Discussion started by: ss_ss
2 Replies

9. Shell Programming and Scripting

Extract string from a file & write to a new file (Perl)

Hi, This is the first time playing around with perl and need some help. Assuming if i have a line of text that looks like this: Date/Time=Nov 18 17:12:11;Device Name=192.168.1.1;Device IP=192.168.1.1;Device Class=IDS;Source IP=155.212.212.111;Source Name=UNKNOWN;Source Port=1679... (3 Replies)
Discussion started by: LuckyGuy
3 Replies

10. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies
Login or Register to Ask a Question