Split string with blancs to pick up information in a file ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split string with blancs to pick up information in a file ?
# 1  
Old 10-24-2011
Split string with blancs to pick up information in a file ?

Hello,

I am quite new in shell, and would like to pick up information in a file.

The file structure is like this faor all data:

Code:
T 50 2 2.5      is this a candy  number  color  price

I know how to pick up a line. I do this:

Code:
head -linenumber candyfile.doc | tail -1

But I would like to know how to split this line to pick up data.

For exemple I need the price at the second line or the number at the fifth, etc...

How can I split with blanck and writ data in a kind of log file like:

Code:
candyfile1.doc : data1 : data2 : data3
candyfile1.doc : data1 : data2 : data3

etc...

thanks for your precious help !
# 2  
Old 10-24-2011
I can't see any relation between the data you're getting in and the data you want out, but splitting on spaces is easy:

Code:
while read A B C D E F G H
do
          echo "First field is $A"
          echo "Second field is $B"
          ...
done < inputfile > outputfile

# 3  
Old 10-24-2011
your solution don't seem to work:

Code:
shadok@pomputer:~$ a=head -3 candyfile.doc | tail -1
shadok@pomputer:~$ echo $a
T 50 2 3.5   is this a candy color price
pomputer:~$ while read $a; do echo "First field is $A"; done

First field is 
^C
shadok@pomputer:~$ while read a; do echo "First field is $A"; done

First field is

So, Where is the problem in what I tried ?
# 4  
Old 10-24-2011
The <inputfile is not optional. Without that, it tries to read from the keyboard instead.

Also, "a" is not the same as "A".

Also, if you list only one field, it will cram everything into that one field. List all the fields instead.
# 5  
Old 10-24-2011
something like this:
Code:
awk '{print FILENAME , $(NF-2),$NF}' OFS=":" infile

# 6  
Old 10-24-2011
Ok,

I can't use these syntax since I need only some lines of my file and on each line some arguments.

So, I just want to know how to split with the blanks, the line that was read and stored in a variable.

if I have
Code:
 a= 12 2 51 6.2 tata titi toto tutu

How can I split a to obtain for exemple the 4th variable 6.2 or the second 2 ?

Sorry if my question wasn't clear...
# 7  
Old 10-24-2011
Code:
a="12 2 51 6.2 tata titi toto tutu"
echo $a |awk '{print $2}' 
echo $a |awk '{print $4}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to split a file with delimited string?

I have a unix file text.txt with below content aaaaa bbbbbbb cccccccccc As of 2013 ddddddddd eeeeeeeeee eeeeeeeee fffffffff As of 2014 gggggggggggg hhhhhhhhh iiiiiiiiiiiiiiii As of 2016 Now I've to split this file with each file ending with line 'As of' . Please suggest how can I do... (6 Replies)
Discussion started by: Steven77
6 Replies

2. UNIX for Dummies Questions & Answers

Obtaining File information based on String Search

Is there a single Command in Unix to get the following Information when searching for files containing one or more strings in a Unix Directory (including sub directories within it) : 1) Complete filename ( path and filename) 2) Owner of the file 3) Size of the file 4) Last Modified date... (3 Replies)
Discussion started by: pchegoor
3 Replies

3. Shell Programming and Scripting

How to Split File based on String?

hi , The scenario is like this, i have a large text files (max 5MB , about 5000 file per day ), Inside almost each line of this file there is a tag 3100.2.22.1 (represent Call_Type) , i need to generate many filess , each one with distinct (3100.2.22.1 Call_Type ) , and one more file to... (3 Replies)
Discussion started by: OTNA
3 Replies

4. Shell Programming and Scripting

A command to split a file into two based on a string

Hello What command can i use to split a tab delimited txt file into two files base on the occurrence of a string my file name is EDIT.txt The content of file is below XX 1234 PROCEDURES XY 1634 PROCEDURES XM 1245 CODES XZ 1256 CODES It has more than a million record If there is... (16 Replies)
Discussion started by: madrazzii
16 Replies

5. Shell Programming and Scripting

Split a fixed length file bases on last occurence of string

Hi, I need to split a file based on last occurece of a string. PFB the explanation I have a file in following format aaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccc ddddddddddddddddddddddddddd 3186rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr... (4 Replies)
Discussion started by: Neelkanth
4 Replies

6. Shell Programming and Scripting

Regex to split a string and write the output in another file.

hi, i am trying to write a script to generate ouput in the following format: ##### buildappi abcd_sh nodebug.##### ##### buildappi ijk_sh nodebug.##### The given string is as follows: xtopSharedDLLs = "abcd_sh def_sh ijk_sh " \ + "jkl_sh any_sh... (15 Replies)
Discussion started by: Rashid Khan
15 Replies

7. Shell Programming and Scripting

awk: split a file using a string problems

Hi, if i use this code awk '/String/{n++}{print > f n}' f=file input I get "input" splited this way file1 String 1515 1354 2356 file 2 String 4531 0345 5345 (3 Replies)
Discussion started by: aloctavodia
3 Replies

8. Shell Programming and Scripting

Split a binary file into 2 basing on 2 delemiter string

Hi all, I have a binary file (orig.dat) and two special delimiter strings 'AAA' and 'BBB'. My binary file's content is as follow: <Data1.1>AAA<Data1.2>BBB <Data2.1>AAA<Data2.2>BBB ... <DataN.1>AAA<DataN.2>BBB DataX.Y might have any length, and contains any kind of special/printable... (1 Reply)
Discussion started by: Averell
1 Replies

9. Shell Programming and Scripting

how to pick out last charactor of a string?

Hi, Does anyone know how many ways to pick out last charactor of a string? Thanks! (7 Replies)
Discussion started by: cin2000
7 Replies

10. Shell Programming and Scripting

split a file at a specified string

to find log files modification, i want to select all the lines of a file behind a string (found by grep ?). :rolleyes: (6 Replies)
Discussion started by: jpl35
6 Replies
Login or Register to Ask a Question