Sponsored Content
Top Forums Shell Programming and Scripting Extract text from file then write variable Post 302848429 by kcpoole on Thursday 29th of August 2013 09:15:37 AM
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...
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
STTY(1) 						      General Commands Manual							   STTY(1)

NAME
stty - set terminal options SYNOPSIS
stty [ option ... ] DESCRIPTION
Stty sets certain I/O options on the current output terminal. With no argument, it reports the current settings of the options. The option strings are selected from the following set: even allow even parity -even disallow even parity odd allow odd parity -odd disallow odd parity raw raw mode input (no erase, kill, interrupt, quit, EOT; parity bit passed back) -raw negate raw mode cooked same as `-raw' cbreak make each character available to read(2) as received; no erase and kill -cbreak make characters available to read only when newline is received -nl allow carriage return for new-line, and output CR-LF for carriage return or new-line nl accept only new-line to end lines echo echo back every character typed -echo do not echo characters lcase map upper case to lower case -lcase do not map case -tabs replace tabs by spaces when printing tabs preserve tabs ek reset erase and kill characters back to normal # and @ erase c set erase character to c. C can be of the form `^X' which is interpreted as a `control X'. kill c set kill character to c. `^X' works here also. cr0 cr1 cr2 cr3 select style of delay for carriage return (see ioctl(2)) nl0 nl1 nl2 nl3 select style of delay for linefeed tab0 tab1 tab2 tab3 select style of delay for tab ff0 ff1 select style of delay for form feed bs0 bs1 select style of delay for backspace tty33 set all modes suitable for the Teletype Corporation Model 33 terminal. tty37 set all modes suitable for the Teletype Corporation Model 37 terminal. vt05 set all modes suitable for Digital Equipment Corp. VT05 terminal tn300 set all modes suitable for a General Electric TermiNet 300 ti700 set all modes suitable for Texas Instruments 700 series terminal tek set all modes suitable for Tektronix 4014 terminal hup hang up dataphone on last close. -hup do not hang up dataphone on last close. 0 hang up phone line immediately 50 75 110 134 150 200 300 600 1200 1800 2400 4800 9600 exta extb Set terminal baud rate to the number given, if possible. (These are the speeds supported by the DH-11 interface). SEE ALSO
ioctl(2), tabs(1) STTY(1)
All times are GMT -4. The time now is 08:47 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy