Split lines in a file and store them in variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split lines in a file and store them in variables
# 1  
Old 07-21-2011
Split lines in a file and store them in variables

Hi,

I have a file called files.txt which contains data like
http://abc.xyz.com/ghi/klm/nop/qrs/tuv/wxyz/
There are multiple lines like the above in this file.

In .sh script, I would like to read this file, split each line, and store the variable of "qrs" and "wxyz" in separate variables.

Please let me know the code. Its very urgent.

Thanks,
Archana






Last edited by archana.n; 07-21-2011 at 04:30 AM..
# 2  
Old 07-21-2011
Code:
 
while read line
do
   var1=`echo $line| nawk -F"\/" '{print $(NF-1)}'`
   var2=`echo $line | nawk -F"\/" '{print $(NF-3)}'`
   echo $var1
   echo $var2
done < files.txt

Input file :

output :

Code:
 
bash-3.00$ ./test.ksh 
wxyz
qrs
sfsd34
asdf
sdf34
qr234s
sdfr34
asdf
sfs43
23sdf

# 3  
Old 07-21-2011
Quote:
Originally Posted by archana.n

In .sh script, I would like to read this file, split each line, and store the variable of "qrs" and "wxyz" in separate variables.

Confused with the above highlighted. Can you be more clear or better post the required output format.
# 4  
Old 07-21-2011
This works for me when I remove 'n' from 'nawk' and '\' from nawk -F"\ /".

Thanks a lot !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to split a large file with the first 100 lines of each condition?

I have a huge file with the following input: Case1 Specific_Info Specific_Info Case1 Specific_Info Specific_Info Case3 Specific_Info Specific_Info Case4 Specific_Info Specific_Info Case1 Specific_Info Specific_Info Case2 Specific_Info Specific_Info Case2 Specific_Info Specific_Info... (2 Replies)
Discussion started by: laurigo
2 Replies

2. Shell Programming and Scripting

Search for a pattern in a file and split the line into two lines

Hi All, Greetings everyone !!! I have a file which has many lines, out of which one line is as below. I need to search for pattern "varchar(30) Select" and if exists, then split the line as below. I am trying to achieve this in ksh. Can anyone help me on this. (8 Replies)
Discussion started by: Pradhikshan
8 Replies

3. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

4. Shell Programming and Scripting

Shell script to read a file and store in variables

I have a input file like this. Sample.txt 30 | TXDatacenter | TXBackupDC 10 | UKDatacenter | UKBackupDC 0 | NLDatacenter | NLBackupDC ...... ...... ...... I need to get these values in different variables like this. Load1=30 PriCenter1=TXDatacenter... (5 Replies)
Discussion started by: Visha
5 Replies

5. Shell Programming and Scripting

split a csv file into specified number of files (not lines)

hi, i really need it ...it's not simple to explain but as it's part of a crontab i can't split the file manually...and the file can change every day so the lines are not a good base. example: how to split 1 csv file in 15 files? thank you very much regards :b: (4 Replies)
Discussion started by: 7stars
4 Replies

6. Shell Programming and Scripting

how to split a huge file by every 100 lines

into small files. i need to add a head.txt and tail.txt into small files at the begin and end, and give a name as q1.xml q2.xml q3.xml .... thank you very much. (2 Replies)
Discussion started by: dtdt
2 Replies

7. Shell Programming and Scripting

split row into lines and insert file name

I have a directory with several hundred files. The file format is a space delimited row with an unknown number of columns: A B C D E F G ... I need to turn this format File1 A File1 B File2 A File3 A File3 B File3 C ... I can use grep to display the filename next to each row of... (2 Replies)
Discussion started by: newreverie
2 Replies

8. Shell Programming and Scripting

Parse config file and store the values in variables

Hi, I have a config file that has blank, commented lines. I need to escape commented lines, blank lines, parse the remaining lines and store them in variables or array. the config file contains the following lines. # config file # Define Oracle User ORA_USER=abcde ORA_PASS=xyzabc... (8 Replies)
Discussion started by: Lakshmi Chowdam
8 Replies

9. Shell Programming and Scripting

Split the single file lines into multiple files

Let's assume that I have a file name called ĎA' and it has 100 lines in it and would like to split these 100 lines into 4 files as specified bellow. INPUT: Input file name A 1 2 3 4 5 6 7 8 9 ........100 Output: 4 output files (x,y,z,w) File x should contains (Skip 4 lines)... (15 Replies)
Discussion started by: subbarao25
15 Replies

10. Shell Programming and Scripting

NAWK array to store lines from huge file

Hi, I would like to clarify about the NAWK array to store multiple lines from huge file. The file is having an unique REF.NO, I wants to store the lines (it may be 100+ lines) till I found the new REF.NO. How can I apply NAWK - arrays for the above? Rgds, sharif. (1 Reply)
Discussion started by: sharif
1 Replies
Login or Register to Ask a Question

Featured Tech Videos