LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
Search this Thread
Old 09-05-2008, 12:55 PM   #1
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Rep: Reputation: 0
(Korn Shell/POSIX) want to make sure last character in line is a space


Hello,

Am on HP UX 11i3, I want to make sure a sensitive file always has a space at the end of each line, seems so simple and yet...


#!/bin/ksh

TEMP_FILE="./test.txt"

# I am interested in the space character just before the closing quote :
echo "123 " >> ${TEMP_FILE}
echo "1234 " >> ${TEMP_FILE}
echo "1235 " >> ${TEMP_FILE}

# I tried this :
while read line
do
echo "...${line#${line%?}}..."
done < ${TEMP_FILE}

## RESULT, expected "... ..." for each line :
#...3...
#...4...
#...5...

# I tried that :
while read line
do
echo "${line}" | sed -e 's/\(^.*\)\(.$\)/\2/'
done < ${TEMP_FILE}

## SAME RESULT :
#...3...
#...4...
#...5...

rm -f ${TEMP_FILE}
exit $?


Any help greatly appreciated?

Thanks much,
Bert
 
Old 09-05-2008, 01:21 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
If you read a record from the file you have to set IFS="\n". This reads records from a file maintaining the trailing space

Code:
#!/bin/ksh

while IFS='\n';read record
do
     
     echo "$record" | grep -q ' $' ||  printf "bad :%s:\n" "$record"
    
done < infile
IFS=" "
infile:
Quote:
this line has a trailing space
this line does not
output:
Quote:
csadev:/home/jmcnama> t.sh
bad :this line does not:
 
Old 09-08-2008, 09:57 AM   #3
quadmore
LQ Newbie
 
Registered: May 2004
Location: Ottawa, Ontario, Canada
Distribution: Redhat 9
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks!

Superb,

That works perfectly. Learned something new today.

Thanks again
Bert
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
korn shell ( ksh ) lechuga Puppy 3 02-05-2007 09:43 PM
what is the Linux command line character for a blank space? Peter Shepard Red Hat 3 06-21-2006 06:25 PM
bash simple test with posix character class osio Programming 5 01-22-2006 07:23 PM
Using a Korn shell in rc scripts desbyleo Solaris / OpenSolaris 3 11-12-2004 05:27 PM
Korn shell script Muzica Solaris / OpenSolaris 4 09-06-2004 12:47 PM


All times are GMT -5. The time now is 01:30 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