Visit Our UNIX and Linux User Community


config file, get field.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting config file, get field.
# 1  
Old 11-08-2009
config file, get field.

Hi Everyone,
I have a txt file: a.txt
Code:
a_b_ 3
c_d_e_ 55

(Assume this a.txt got 100 lines, here i just list 2 lines)
How should i get the 2nd field when 1st field eq c_d_e_. my way is:
Code:
`cat a.txt | grep c_d_e_ | cut -f2 -d' '`

Is there simple way, more efficient way in regular expression, perl? awk? and etc. Smilie
Thanks
# 2  
Old 11-08-2009
Are you saying the fields are split on spaces?

If so, it's trivial with awk or Perl and even with just bash:

Code:
while read line
do
set $line
echo $2
done

# 3  
Old 11-08-2009
Quote:
Originally Posted by jimmy_y
(...)
How should i get the 2nd field when 1st field eq c_d_e_. my way is:
Code:
`cat a.txt | grep c_d_e_ | cut -f2 -d' '`

Is there simple way, more efficient way in regular expression, perl? awk? and etc. Smilie
Thanks
better (don't use cat with grep !)
Code:
grep c_d_e_ a.txt | cut f2 -d' '

# 4  
Old 11-08-2009
Examples of awk and sed would be
Code:
$(awk '/c_d_e_/{print $2}' a.txt)

or
Code:
$(sed -n 's/^c_d_e_ *//p' a.txt)


Last edited by Scrutinizer; 11-08-2009 at 03:09 PM..
# 5  
Old 11-08-2009
Thanks Scrutinizer, frans, Tony.
# 6  
Old 11-08-2009
to be even more specific since you are checking field 1
Code:
awk '$1=="c_d_e_"{print $2}' file


Previous Thread | Next Thread
Test Your Knowledge in Computers #42
Difficulty: Easy
In general, cookies are created by web clients and stored on web servers.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

4. Red Hat

Apache virtual host config vs global config problem

Hi folks, I am trying to configure Apache webserver and also a virtual host inside this webserver. For Global server config: /var/www/html/index.html For virtual host config: /var/www/virtual/index.html Both client10 & www10 are pointing to 192.168.122.10 IP address. BUT, MY... (1 Reply)
Discussion started by: freebird8z
1 Replies

5. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

6. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

7. Shell Programming and Scripting

Append 1st field from a file into 2nd field of another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (1 Reply)
Discussion started by: amurib
1 Replies

8. Shell Programming and Scripting

Appending 1st field in a file into 2nd field in another file

Hi, I've internally searched through forums for about 2+ hours. Unfortunately, with no luck. Although I've found some cases close to mine below, but didn't help so much. Actually, I'm in short with time. So I had to post my case. Hoping that you can help. I have 2 files, FILE1 ... (0 Replies)
Discussion started by: amurib
0 Replies

9. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

10. Shell Programming and Scripting

parsing config file to create new config files

Hi, I want to use a config file as the base file and parse over the values of country and city parameters in the config file and generate separate config files as explained below. I will be using the config file as mentioned below: (config.txt) country:a,b city:1,2 type:b1... (1 Reply)
Discussion started by: clazzic
1 Replies

Featured Tech Videos