Different FS for each pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Different FS for each pattern
# 1  
Old 07-26-2010
Different FS for each pattern

I have a source that is predominantly delimited by whitespace, so the standard FS is fine. But a single line in the source would be more useful if I could break it up by the '/' character. Is there a way to redefine the FS on a pattern by pattern basis?

Failed example...
Code:
/Address/ { OFS=FS
FS="/"
robot=$4
FS=OFS}
/^[a-z]/ {name=$1}
/pkg_version/ {print robot " " name " " $4}


Last edited by Franklin52; 07-26-2010 at 04:21 PM.. Reason: Please use code tags
# 2  
Old 07-26-2010
Can you post some lines of your input file and the desired output?

Please use code tags!
# 3  
Old 07-26-2010
Code:
======================================================
Address: /Alpha/wys02/ly15.example.net  Request: probe_list
======================================================
controller      PDS_PDS         385
 name            PDS_PCH          11 controller
 description     PDS_PCH          34 Robot process and port control~
 group           PDS_PCH           7 NimBUS
 active          PDS_I             2 1
 type            PDS_I             2 0
 command         PDS_PCH          11 controller
 config          PDS_PCH          10 robot.cfg
 logfile         PDS_PCH          15 controller.log
 workdir         PDS_PCH           6 robot
 timespec        PDS_PCH           1
 times_activated PDS_I             2 0
 last_action     PDS_I             2 0
 pid             PDS_I             5 5642
 times_started   PDS_I             2 1
 last_started    PDS_I            11 1278939246
 pkg_name        PDS_PCH          13 robot_update
 pkg_version     PDS_PCH           5 3.52
 pkg_build       PDS_PCH           2 1

# 4  
Old 07-26-2010
Try :
Code:
/Address/ { OFS=FS
FS="/"
$0 = $0 "";
robot=$4
FS=OFS}
/^[a-z]/ {name=$1}
/pkg_version/ {print robot " " name " " $4}

Jean-Pierre.
# 5  
Old 07-26-2010
use split.

Code:
awk '/Address/ {split($0,robot,"/"); print robot[4]}' urfile

You code can be updated as:

Code:
/Address/ {split($0,robot,"/")}
/^[a-z]/ {name=$1}
/pkg_version/ {print robot[4] " " name " " $4}

# 6  
Old 07-27-2010
Both worked flawlessly. Thanks for the tips!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

7. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question