Visit Our UNIX and Linux User Community


Map a string in a the middle of a record


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Map a string in a the middle of a record
# 1  
Old 05-15-2014
Map a string in a the middle of a record

Hi Folks!

I hope you can help me figure this out. I need to print a record which contains the contents of a config file. The contents of the config file should be found within the 21st and the 30th position of the fixed width reference file.

Config File:
Code:
aaaa
bbbb
cccc

Reference File:
Code:
123456789           bbbb      234545
123454567           cccc      23452  
1234567890sas       dddd      1234
124345677           bbbb      123445

Expected Output:
Code:
123456789           bbbb      234545
123454567           cccc      23452  
124345677           bbbb      123445

I hope the gurus can help me
~kokoro
# 2  
Old 05-15-2014
Code:
$> cat /tmp/a.txt
123456789           bbbb      234545
123454567           cccc      23452
1234567890sas       dddd      1234
124345677           bbbb      123445

$> cat /tmp/b.txt
aaaa
bbbb
cccc

$> fgrep -w -f /tmp/b.txt /tmp/a.txt
123456789           bbbb      234545
123454567           cccc      23452
124345677           bbbb      123445

# 3  
Old 05-15-2014
Code:
awk 'BEGIN{
FIELDWIDTHS = "20 10 100"}
NR == FNR {a[$1]; next}
{t = $2;
gsub(" ", "", t);
if(t in a)
  print}' conf ref

# 4  
Old 05-15-2014
Quote:
Originally Posted by kokoro
I need to print a record which contains the contents of a config file. The contents of the config file should be found within the 21st and the 30th position of the fixed width reference file.

Config File:
Code:
aaaa
bbbb
cccc

Reference File:
Code:
123456789           bbbb      234545
123454567           cccc      23452  
1234567890sas       dddd      1234
124345677           bbbb      123445

If your reference file always looks like this (that is, the fields are surrounded by ample whitespace AND the search patterns will not appear somewhere else on the line), itkamarajs solution is probably the easiest and fastest.

If the file could contain lines like this:

Code:
123456789           bbbb      234545
123454very_long_linecccc      23452

where it is not possible to match for a word and the search pattern is distinguishable only by the position on the line you will have to do a bit more, for instance a shell loop:

Code:
#! /bin/ksh
typeset PATTERN=""
cat /path/to/config.file |\
while read PATTERN ; do
     grep '^.\{20\}'"${PATTERN}" /path/to/reference.file
done
exit 0

This will "anchor" the pattern to character position 21 in the line by searching for "20 characters followed by $PATTERN".

If the search patterns contain regexp metacharacters (like "*", ".", etc.) you might have to take evn more precautions by escaping these (precede them with a backslash, i.e. "*" -> "\*").

Taking all these precautions will not hurt, even if they are unnecessary, but they might slow down the process, soanalyze your data carefully if they are necessary or not.

I hope this helps.

bakunin
 

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

2. Linux

Remove newline in middle of string

my file input is with tab as delimiter, and in every line, there would be a skip of line with an unexcepted newline breaker. I'd like to remove this \n and put the information in the same line. INPUT a1 b1b2 c1 c2 d1 a2 b3 c3 d4 OUTPUT a1 b1b2 c1c2 ... (9 Replies)
Discussion started by: kinkichin
9 Replies

3. Shell Programming and Scripting

Split string into map (Associative Array)

Hi Input: { committed = 782958592; init = 805306368; max = 1051394048; used = 63456712; } Result: A map (maybe Associative Array) where I can iterate through the key/value. Something like this: for key in $map do echo key=$key value=$map done Sample output from the map: ... (2 Replies)
Discussion started by: chitech
2 Replies

4. Shell Programming and Scripting

mappin strings of two different file and finding the mapped string and then map other fields.

As i am new to unix so facing some problems in scripting: here is my question: i m having two files. 1st file say a.txt contain 3 column like SPECIALITY|UMP_CODE|SPECIALTY_CODE Addictive Diseases|25ADD|ADD Addictive Diseases/Family Practice|25ADD|ADD/FP Aerospace Medicine|1.041666667|AM... (4 Replies)
Discussion started by: dsh007
4 Replies

5. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

6. UNIX for Dummies Questions & Answers

Splitting a string and putting another string in the middle?

Hello all! I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension). To illustrate... Before: filename.txt anotherfilename.txt After: filename_20090724.txt anotherfilename_20090724.txt ... (7 Replies)
Discussion started by: jisoo411
7 Replies

7. UNIX for Dummies Questions & Answers

Add string to middle of a file

Hi, I want to write a script that takes a file and a string as params and adds the string to the middle line of the file. Also, I want to output the results back to the original file passed without using temp files. I am very much new to UNIX so this is all a little like black magic to me at... (15 Replies)
Discussion started by: Chiefos
15 Replies

8. Shell Programming and Scripting

stripping certain characters in at the middle of a string

I am trying to strip out certain characters from a string on both (left & right) sides. For example, line=see@hear|touch, i only want to echo the "hear" part. Well i have tried this approach: line=see@hear|touch templine=${line#*@} #removed "see@" echo ${templine%%\|*} #removed... (4 Replies)
Discussion started by: mcoblefias
4 Replies

9. Shell Programming and Scripting

add a string in the middle of the file

i want to add a string in a very top of a file without using VI or SED or AWK this is what ive done: (echo '0a'; echo 'LINE OF TEXT'; echo '.'; echo 'wq') | ed -s myfile to add astrng right in the middle i could have count the lines of the file and just chenge the address. ... (6 Replies)
Discussion started by: ciroredz
6 Replies

10. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies

Featured Tech Videos