awk RS declaration with \n


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk RS declaration with \n
# 1  
Old 03-14-2017
awk RS declaration with \n

Hello Team,
input text:
Code:
'sysserver(cells/SOL/nodes/bclab130node15/servers/sysserver|server.xml)\nserver2(cells/SOL/nodes/bclab130node15/servers/server2|server.xml)\nfmserver(cells/SOL/nodes/bclab130node15/servers/fmserver|server.xml)\ncmserver(cells/SOL/nodes/bclab130node15/servers/cmserver|server.xml)\nintgserver(cells/SOL/nodes/bclab130node15/servers/intgserver|server.xml)\nitsmserver(cells/SOL/nodes/bclab130node15/servers/itsmserver|server.xml)\nsysserver(cells/SOL/nodes/bclab130node14/servers/sysserver|server.xml)\nserver2(cells/SOL/nodes/bclab130node14/servers/server2|server.xml)\nfmserver(cells/SOL/nodes/bclab130node14/servers/fmserver|server.xml)\ncmserver(cells/SOL/nodes/bclab130node14/servers/cmserver|server.xml)\nintgserver(cells/SOL/nodes/bclab130node14/servers/intgserver|server.xml)\nitsmserver(cells/SOL/nodes/bclab130node14/servers/itsmserver|server.xml)'

here i want to specify the awk input record separator as \n (plain string as in the input text above) and FS as "[\'\(]" and get only the first field of all the lines/records.

Sample output:
Code:
sysserver
server2
fmserver
cmserver
intgserver
itsmserver
sysserver
server2
fmserver
cmserver
intgserver
itsmserver

Thanks,
Chandana

---------- Post updated at 02:45 PM ---------- Previous update was at 02:39 PM ----------

the header should be "awk RS declaration with \n"

Last edited by RudiC; 03-14-2017 at 06:34 AM..
# 2  
Old 03-14-2017
Try
Code:
SQ="'"
sed -e "s/$SQ//g" -e 's/\\n/\n/g; s/([^)]*)//g ' file
sysserver
server2
fmserver
cmserver
intgserver
itsmserver
sysserver
server2
fmserver
cmserver
intgserver
itsmserver

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-14-2017
Linux

Thanks RudiC, that works perfect.

i want to understand
Code:
([^)]*

a little more, Would you mind explaining?

Also can this be done with awk, by setting RS built-in variable?

Last edited by chandana.hs; 03-14-2017 at 07:13 AM..
# 4  
Old 03-14-2017
That regex you are citing is missing the final ) . It is looking for a ( , zero or more non- ) , and a closing ) , i.e. it will (hopefully) match and eliminate the trailing parenthesized term in each record.
The reason I did not propose an awk solution is that not all versions allow for multi-char FS nor RS.
# 5  
Old 03-14-2017
This might work in awk:
Code:
awk '{gsub (/\\n/, RS); gsub (/\([^)]*\)|\047/, _)} 1' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 03-14-2017
A convoluted longhand method, OSX 10.12.3, using dash in default terminal.
Code:
#!/usr/local/bin/dash
echo 'sysserver(cells/SOL/nodes/bclab130node15/servers/sysserver|server.xml)\nserver2(cells/SOL/nodes/bclab130node15/servers/server2|server.xml)\nfmserver(cells/SOL/nodes/bclab130node15/servers/fmserver|server.xml)\ncmserver(cells/SOL/nodes/bclab130node15/servers/cmserver|server.xml)\nintgserver(cells/SOL/nodes/bclab130node15/servers/intgserver|server.xml)\nitsmserver(cells/SOL/nodes/bclab130node15/servers/itsmserver|server.xml)\nsysserver(cells/SOL/nodes/bclab130node14/servers/sysserver|server.xml)\nserver2(cells/SOL/nodes/bclab130node14/servers/server2|server.xml)\nfmserver(cells/SOL/nodes/bclab130node14/servers/fmserver|server.xml)\ncmserver(cells/SOL/nodes/bclab130node14/servers/cmserver|server.xml)\nintgserver(cells/SOL/nodes/bclab130node14/servers/intgserver|server.xml)\nitsmserver(cells/SOL/nodes/bclab130node14/servers/itsmserver|server.xml)' > /tmp/text
# cat /tmp/text
# Code proper starts here.
while read -r LINE
do
	echo "${LINE%(*}"
done < /tmp/text > /tmp/newtext
# Code end.
cat /tmp/newtext
exit

Results:-
Code:
Last login: Tue Mar 14 10:08:07 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./single_line.sh
sysserver
server2
fmserver
cmserver
intgserver
itsmserver
sysserver
server2
fmserver
cmserver
intgserver
itsmserver
AMIGA:amiga~/Desktop/Code/Shell> _


Last edited by wisecracker; 03-14-2017 at 11:31 AM.. Reason: Change code for simpler method.
This User Gave Thanks to wisecracker For This Post:
# 7  
Old 03-16-2017
Hi Rudic,

Thank you for the solution,
And again , one more query, What does the _ & number 1 mean towards the end?

Code:
awk '{gsub (/\\n/, RS); gsub (/\([^)]*\)|\047/, _)} 1' file

Thank you.
Chandana
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function prototype declaration

Hi All, I have the script as below #!bin/bash let k=9 if then echo "Start" Hello echo "End" else echo "failed" fi function Hello() { echo "hello !!!!" } I got the below error : (4 Replies)
Discussion started by: Balasankar
4 Replies

2. Shell Programming and Scripting

variable declaration

how to check 1. If variable is declared or not 2. If any value if assigned to variable or not. in UNIX shell script (6 Replies)
Discussion started by: ace_friends22
6 Replies

3. Programming

DIR declaration

are you suppose to only use one DIR declarations, for example DIR *dir_ptr; because I'm declaring mine like so: DIR *dir_ptr_src, *dir_ptr_dest; and using it like so: if( ( dir_ptr_src = opendir ( av ) ) != NULL && ( dir_ptr_dest = opendir( av ) ) != NULL ) /* check if... (3 Replies)
Discussion started by: l flipboi l
3 Replies

4. Shell Programming and Scripting

variable declaration

Hi Guys, What does this mean actually ? Can somebody give me any explanation ? x=${x:=1} Thanks (2 Replies)
Discussion started by: amit.behera
2 Replies

5. Shell Programming and Scripting

Perl variable declaration

what is the meaning of this particular line of code in perl. my %global_port2lanid = (); (2 Replies)
Discussion started by: suvenduperl
2 Replies

6. Shell Programming and Scripting

Array Declaration and For Loop

I am just stucked in syntax.This is more like a array and for loop problem. I want to use ls -l command and get filezise and filename of all filenames in the directory in an array (say array#1). After 2 minutes of sleep, i want to get the same information in another array (say array#2). The... (4 Replies)
Discussion started by: 33junaid
4 Replies

7. Shell Programming and Scripting

awk field declaration??

Hi everybody: Could anybody tell me how should I declare a empty fieldinto a if stament? Is it possible like this?: if ($field == " ") body Thanks in advance and cheers!! . :D (1 Reply)
Discussion started by: tonet
1 Replies

8. Shell Programming and Scripting

double variable declaration

i have variables with different values as below in KSH AU_Holiday=1 SG_Holiday=0 KR_Holiday=1 JP_Holiday=0 $country_cd is the second variable which be one of 'AU' 'SG' 'KR' 'JP' which comes in a for loop. form that i need to derive the variable like first one and check whether it is equal... (3 Replies)
Discussion started by: kotasateesh
3 Replies

9. Shell Programming and Scripting

Help with variable declaration

I declared a variable x that gets the count(*) from a table. The table name is also defined as a variable. What's wrong with this statment : X=” select count(*) from ${table_name}“ then y = `${X}${table_name}' echo ${y} It throws an error saying count not found. Please... (1 Reply)
Discussion started by: dsravan
1 Replies

10. Programming

Variable declaration

what does this mean when a variable is declared as register int i; Thanks. :confused: (2 Replies)
Discussion started by: laila63
2 Replies
Login or Register to Ask a Question