shell script

March 5, 2010 at 07:24:32
Specs: AIX
Hi,
I wrote following code in unix

ls -1 | while read
do
sed 's/^M//g' $file > ${file}.temp
mv ${file}.temp $file
done

it does not work but when i write

for i in `ls`
do
sed 's/^M//g' $file > ${file}.temp
mv ${file}.temp $file
done

it work.

Please advise whats wrong with first script



See More: shell script

Report •


#1
March 5, 2010 at 09:34:20
First, the while/read statement requires a variable; this:

ls -1 | while read

should be:

# choose your own variable_name
ls -1 | while read variable_name.

Second, I do not see the point of your loop. Variable $file is not defined as you did not post it. You are just executing:

sed 's/^M//g' $file > ${file}.temp

for each file/directory in your present working directory???


Report •

#2
March 8, 2010 at 04:41:47
Thanks@ nails

I had ported certain files from windows to aix.I found that after porting all the files had control character so i was for a script that could remove the control character in all the files in present working directory in one go..


Report •

#3
March 8, 2010 at 06:08:45
this example will delete all newlines from files and put the altered output into dir. xx:
mkdir xx
for a in `ls -1` ; do tr -d "\012" < $a > /xx/$a ; done

Report •

Related Solutions

#4
March 8, 2010 at 07:20:37
This is a common problem. Most unix/linux variations have a dos2unix (and conversely a unix2dos) command. Take a look at this link:

http://www.tek-tips.com/faqs.cfm?fi...


Report •

#5
March 8, 2010 at 07:32:00
thanks a lot folks
i could run those scripts with ur inputs. :)

ls -1 | while read file
do
sed 's/^M//g' $file > ${file}.temp
mv ${file}.temp $file
done
===========

for file in `ls`
do
sed 's/^M//g' $file > ${file}.temp
mv ${file}.temp $file
done
----------------
mkdir xx
for a in `ls -1` ; do tr -d '\015'< $a > /xx/$a ; done

-------
'\015' for ^M symbol

'\012' for new line

also ^M used in sed is typed by pressing ctrl v + ctrl m

On AIX dos2unix is not there..

--------------------------
Thanks once again



Report •


Ask Question