Extract from txt file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract from txt file
# 1  
Old 06-16-2009
Java Extract from txt file

I have data as follow in the txt file.

I want to skip line starting with '#' sign.

#command program
abc defmt
exp refmt
... ...

I want to store abc exp .... in a array.
I want to store defmt refmt in a array

I need command to read each line in the file.
I need command to parse data in each line.
I need command to skip line starting with #.

Any basic clue will be sufficient I will build on top of it.

Thanks a lot in a advance.
# 2  
Old 06-16-2009
Tools For what purpose?

Your request has the 'look and feel' of homework? To merely read data and store to an array does not serve any purpose. Please explain what you are trying to do.
# 3  
Old 06-16-2009
I want to build menu item from the text file and excute command from the variable array.
# 4  
Old 06-16-2009
Hammer & Screwdriver OK, maybe this will get you started...

Code:
> cat file25
# disregard this data
aaa choice_a
bbb choice_b
ccc choice_C
ddd choice_d
# forget this line
eee choice_e

> cat play25.sh
awk '
  { 
    if ($1!="#")
      { 
#      print $0 
      opt[$1]=$1
      choice[$1]=$2
      }
  }
  END {
    for (i in opt)
    print "Option="opt[i]" and value ="choice[i]

  }
' file25

> play25.sh
Option=aaa and value =choice_a
Option=ccc and value =choice_C
Option=eee and value =choice_e
Option=bbb and value =choice_b
Option=ddd and value =choice_d

# 5  
Old 06-16-2009
Thanks a lot for the script.

I tried this. If I want opt and choice array available to me outside awk then what I need to do.

Basically I want to use this array in the script following awk command.

Any tip ...

Last edited by ekb; 06-16-2009 at 03:22 PM.. Reason: More desc
# 6  
Old 06-16-2009
Quote:
Originally Posted by ekb
...
If I want opt and choice array available to me outside awk then what I need to do.
Basically I want to use this array in the script following awk command.
...
Code:
$
$ cat data.txt
# a comment here
aaa menu_item_1
bbb menu_item_2
ccc menu_item_3
$
$ cat testscr.sh
#!/usr/bin/bash
opt=(`sed -n '/^[^#]/p' data.txt | cut -d" " -f1`)
menuitem=(`sed -n '/^[^#]/p' data.txt | cut -d" " -f2`)
for i in ${opt[@]}; do
  echo "Option = $i"
done
for i in ${menuitem[@]}; do
  echo "Menuitem = $i"
done
$
$ . testscr.sh
Option = aaa
Option = bbb
Option = ccc
Menuitem = menu_item_1
Menuitem = menu_item_2
Menuitem = menu_item_3
$
$

tyler_durden
# 7  
Old 06-17-2009
Code:
while(<DATA>){
	next if /^#/;
	my @tmp=split(/\s+/,$_);
	push @arr1, $tmp[0];
	push @arr2, $tmp[1];
}
print join "|", @arr1;
print "\n";
print join "|", @arr2;
__DATA__
#command program
abc defmt 
exp refmt
#another command line
bbb defmt
ccc defmt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To extract values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 ... (1 Reply)
Discussion started by: jeo_fb
1 Replies

2. Shell Programming and Scripting

Extract information from txt file

Hello! I need help :) I have a file like this: AA BC FG RF TT GH DD FF HH (a few number of rows and three columns) and I want to put the letters of each column in a variable step by step in order to give them as input in another script. So I would like to obtain: for the 1° loop:... (11 Replies)
Discussion started by: edekP
11 Replies

3. Shell Programming and Scripting

Extract the filename and write to .txt

I'm new to this forum and also to UNIX scripting. I need a command to extract the filename from the path and write to .txt file. Thanks in advance for your guidance. (23 Replies)
Discussion started by: Ram Kumar_BE
23 Replies

4. Shell Programming and Scripting

Script extract text from txt file with grep

All, I require a script that grabs some text from the gitHub API and will grep (or other function) for a string a characters that starts with (") quotes followed by two letters, may contain a pipe |, and ending with ) . What i have so far is below but it's not returning anything. ... (4 Replies)
Discussion started by: ChocoTaco
4 Replies

5. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

6. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

7. Shell Programming and Scripting

extract words from txt using perl

Hi, i will deal with txt file and i want to use perl to extract number of words from this txt ex :if the txt file is a story which contains person names and iwant to extract these names and there is something else that these names which i want perl to extract must match the words (person names) ... (2 Replies)
Discussion started by: eng_shimaa
2 Replies

8. Shell Programming and Scripting

Extract content from several txt-files

Hi! Im trying to write a script in ksh that creates a single txt-file from specific content in several other txt-files. From these files I want to extract all text after 'WORD' and before '=', regardless of number of lines and other content. I have tried cat and guess I need... (7 Replies)
Discussion started by: larsu
7 Replies

9. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

10. Programming

c file to extract real value from a txt file

Hello Friends,, I m really a new bee to C programms , please help me with a code.. I found some theads here similar to this but Not able to solve what exactly I want.. suppose I ve txt file as below. abc.txt 12 23 10 11 131 159 12.2 13.8 Then I want to... (7 Replies)
Discussion started by: user_prady
7 Replies
Login or Register to Ask a Question