LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 05-01-2004, 09:57 PM   #1
test
Newbie
 
Registered: Apr 2004
Posts: 14

Rep: Reputation: 0
problems with newline in awk


Hi, I'm trying to make a script that reads from a file, if the words "ADD" occures the function AddName should be called with the name that follows as an argument

The problem is that awk cuts away the newline or something like that. Why?? Can someone help me?


# This is the file i read from
Blablabla;test
ADD;Tom
blabla;ok
ADD;Mark


# This is the script
function AddName()
{
echo "Added"
}
temp=`awk -F";" '
{
if ( $0 ~/^'"#"'/ )
{
# Ignore "#"
}
else
{
if ( $1 == "ADD" )
{
print "AddName "$2
}
fi
}' file`
$temp
exit 0

The output if I echo it would be:
AddName Tom AddName Mark

I want it to be:
AddName Tom
AddName Mark
 
Old 05-01-2004, 10:02 PM   #2
TheOther1
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Distribution: RHAS 2.1, RHEL3, RHEL4, SLES 8.3, SLES 9, SLES9_64, SuSE 9.3 Pro, Ubuntu, Gentoo
Posts: 335

Rep: Reputation: 32
I think you just need to add \n to the end of the
print "AddName "$2
line to read:
print "AddName "$2\n

Don't have a linux box to test on right now, so this is off the top of my head.
 
Old 05-01-2004, 10:16 PM   #3
test
Newbie
 
Registered: Apr 2004
Posts: 14

Original Poster
Rep: Reputation: 0
Nope, that just returned the error: "backslash not last character on line"

I also tried:
print "AddName "$2"\n"
printf "AddName "$2
printf "AddName "$2\n
printf "AddName "$2"\n"

\n seems to output a <space>
now i have tried with \f and \r as well. With \f\r it came on seperate lines when i echoed it, but it would not run, I think because in "reality" it is still on one line.. Any other suggestions?

Last edited by test; 05-01-2004 at 10:34 PM.
 
Old 05-02-2004, 12:00 AM   #4
test
Newbie
 
Registered: Apr 2004
Posts: 14

Original Poster
Rep: Reputation: 0
I read something about ORS(Output record seperator), and tried this

ORS="\n"
print "AddName "$2

but i did not help.

I,m stuck here, can someone help please???
 
Old 05-02-2004, 12:26 AM   #5
rkef
Member
 
Registered: Mar 2004
Location: bursa
Posts: 110

Rep: Reputation: 15
Uhh... have you tried: print "AddName $2\n" ?
 
Old 05-02-2004, 12:41 AM   #6
test
Newbie
 
Registered: Apr 2004
Posts: 14

Original Poster
Rep: Reputation: 0
I have now, but that didn't do the trick... same output.
 
Old 05-02-2004, 02:33 AM   #7
test
Newbie
 
Registered: Apr 2004
Posts: 14

Original Poster
Rep: Reputation: 0
I got help somewhere else...
The solution if someone wonders:
function AddName()
{
echo "Added"
}
temp=`awk -F";" '
{
if ( $0 ~/^'"#"'/ )
{
# Ignore "#"
}
else
{
if ( $1 == "ADD" )
{
print "AddName "$2
}
fi
}' file`

OLDIFS="$IFS"
IFS=$'\n'
for command in $temp; do
$command
done
IFS="$OLDIFS"
exit 0
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newline problem harrylmh Programming 4 11-21-2005 04:04 AM
php + newline??? blizunt7 Programming 1 06-19-2005 11:44 PM
Newline in xmessage? scottjwoodford Linux - General 2 06-09-2005 03:17 PM
Removing newline. Tarts Programming 2 10-16-2003 11:49 AM
What is a newline? raptorsoft2000 Linux - Newbie 6 08-04-2003 10:05 PM


All times are GMT -5. The time now is 08:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration