searching through a file and replacing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers searching through a file and replacing
# 1  
Old 09-30-2005
searching through a file and replacing

Hi can anyone show me how to search through a file (multiple columns) and based on say one column if empty and another being popluted and if both senario's are true then get information from another file matching the data from the populated column based upon the populated column and bring through details adjacent to this column....sort of like a vlookup in MS Excel only I'd need to sort first on the blanks, then the populated column, then do a vlookup...which ofcourse works but I need to be able to do this with muy server--- DG-Unix

many thanks in advance
# 2  
Old 10-02-2005
Take a simple example. Say you have two files...
Code:
==> file1 <==
key1,value1
key2,value2
key3,
key4,value4
key5,value5

==> file2 <==
key1,value1b
key2,value2b
key3,value3b
key4,value4b
key5,value5b

You can use awk to lookup the missing values...
Code:
awk '
  BEGIN {
      OFS = FS = ","
      while (getline < "file2" > 0)
          a[$1] = $2
  }
  {  
      if ($1 && !$2)
          $2 = a[$1]
      print
  }
  ' file1 > file3

The resulting file looks like this...
Code:
==> file3 <==
key1,value1
key2,value2
key3,value3b
key4,value4
key5,value5

# 3  
Old 10-04-2005
Thanks for that ...But is there no way of doing a lookup ...meaning if $string found then hunt through another file and retrieve the matching adjacant line
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching file for pattern, output to file (BASH)

Hello, I'm trying to write a script in Bash to assist in pentesting. Essentially I'm looking to use a script to search for some terms in a log file and then send that key information into another file. The log files consist of HTTP and SSL information that someone creates while browsing and... (2 Replies)
Discussion started by: Sagesparten007
2 Replies

2. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

3. Shell Programming and Scripting

Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus, I am new to shell scripting and in the process of learing. I am trying to find whether a file name has today's date in MMDDYYYY format. I am using the following code and it doesn't seem like working. #!/usr/bin/ksh today=$(date '+%m%d%Y') echo today: $today file=`find... (4 Replies)
Discussion started by: shankar1dada
4 Replies

4. Shell Programming and Scripting

searching multiple lines and replacing in shell scripting

Hi, I have a file with below contents, ssenthil = rw anilkg = rw I want to search for "ssenthil" and need to delete line 1 and 2 , if the third line starts with "" respectively and blank line immediately and third line starts with " anilkg = rw Please help me . Great day... (5 Replies)
Discussion started by: anil8103
5 Replies

5. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

6. Shell Programming and Scripting

Searching for Log / Bad file and Reading and writing to a flat file

Need to develop a unix shell script for the below requirement and I need your assistance: 1) search for file.log and file.bad file in a directory and read them 2) pull out "Load_Start_Time", "Data_File_Name", "Error_Type" from log file 4) concatinate each row from bad file as... (3 Replies)
Discussion started by: mlpathir
3 Replies

7. Shell Programming and Scripting

searching & replacing/removing only certain HTML tags

I generally save a lot of web pages for reading offline which works out great for school. Now I have to spend a lot of time on the bus and I am looking for the best way to read some of these webpages using my Nokia 7610. I have uploaded the files to my phone, but they are deadly deadly slow to... (2 Replies)
Discussion started by: naphelge
2 Replies

8. Shell Programming and Scripting

searching a date and replacing with another date

I have a text file that i want to search through and pick out any dates that are formatted like MM/DD/YYYY and replace them with a date i want like 10/29/2009. any idea show i would do this?:) Snapshot of my text file: test4>s44syd5172>070>528>ENU>nongnuan>wanrawee>sr2330532>... (7 Replies)
Discussion started by: infiant
7 Replies

9. Shell Programming and Scripting

Searching and replacing '\M' in file

Hi All, I am new to shell scripting, please help! On regular basic we get file in different directory which contains either \M in the file at the starting of the line or at the end. I have to replace the same with blank. I have tried writing the script however it's not working,... (5 Replies)
Discussion started by: kumarmani
5 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question