Newbye. Help with KSH. Loop in files and remove first line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Newbye. Help with KSH. Loop in files and remove first line
# 1  
Old 10-19-2008
Newbye. Help with KSH. Loop in files and remove first line

Hi everybody. Firstly, sorry for doing such a basic questions, but i have never worked with linux shells and at this moment i am trully desperated :d.

I have been checkin' another posts of this forum looking for different things for mixing them and get the solution to my problem, but i have not been yet able to reach something that works.

My problem is that i have to do a ksh script for processing all the files in a directory given, remove the first line for each of them, saving them with the same name (but i'm so desperated that i will save them with another name too ).

For example, the /home/bringer/test/files directory has the following files:

input1.txt
input2.txt
input3.txt
...
input99.txt

and i want to remove the first line for each of them. I have the following script, but it doesn' work Smilie

Code:
#!/bin/ksh
inDir=$1 #Input Directory
outDir=$2 #output directory
timeStmpExpr='*.TXT'
echo 'Just before ls 11111'
ls ${inDir}
echo 'Just before ls-grep 22222'
ls ${inDir} | grep $timeStmpExpr
echo 'Just before ls-grep-while 22222'
ls ${inDir} | grep $timeStmpExpr | \
echo ${inDir}
#while read inFile
for inFile in ${inDir}
do
  echo "inside loop==========================="
  mFullInName=${inDir}'/'${inFile}
  FName=`echo ${inFile} | sed "s/\(.*\)${timeStmpExpr}/\1/"`
  #mFullOutName=${outDir}'/'${FName}'.txt'
   mFullOutName=${outDir}'/'${FName}'.txt'
  echo 'inFile = '${inFile}' outFile = '${outFile}' FName = '${FName}
  echo 'mFullInName = '${mFullInName}' mFullOutName = '${mFullOutName}
  sed '1d;$d' ${mFullInName} > ${mFullOutName}
  rm -f ${mFullInName}
done
echo "FIle copying Completed"

I'm really stucked and i don't know the right with to continue. May anybody give me any clues?

Thanks a lot in advance
# 2  
Old 10-19-2008
Try:

I assume that you are in the directory where all the files are available :

Code:
for each in *; do sed -n '2,$p' $each>$each.tmp; mv $each.tmp $each; done

# 3  
Old 10-19-2008
It works great, but i'm trying something different:

Code:
#!/bin/ksh
inDir=$1 #Input Directory
echo 'Just before'
for each in ${inDir}
do
 echo "Inside"
 sed -n '2,$p' $each>$each.tmp; mv $each.tmp $each;
done
echo "Hecho"

I have taken your sample and added the possibility to give it the directory by standard input, but after call it with this "ksh ejemplo2.ksh /home/bringer/test/ficheros" i get this:

Quote:
Just before ls 11111
Inside
sed: error reading from /home/bringer/test/ficheros/: it's a directory
mv: «/home/bringer/test/ficheros/.tmp» and «/home/bringer/test/ficheros/.tmp» are the same file
Hecho
How can i modify it to make it works?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ksh: Read line parse characters into variable and remove the line if the date is older than 50 days

I have a test file with the following format, It contains the username_date when the user was locked from the database. $ cat lockedusers.txt TEST1_21062016 TEST2_02122015 TEST3_01032016 TEST4_01042016 I'm writing a ksh script and faced with this difficult scenario for my... (11 Replies)
Discussion started by: humble_learner
11 Replies

2. Shell Programming and Scripting

ksh Loop through file one, remove lines from file two

Good Afternoon, I start with a file named biglist.txt. I have another file smallerlist. txt I want to remove the lines from smallerlist.txt from biglist.txt and leave those lines that do not reside in smallerlist.txt. Thanks !! (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

How to loop a text file to remove the old files?

Hi , I'm using Ksh 88 version. I've a text file which contains the directory names DIR1 DIR2 ---- DIR10 I've to remove the data which has more that 30 days from the above directories . The data has to be removed from the following path cd /home/etc/DIR1/hist cd /home/etc/DIR2/hist... (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

scp list of files using FOR LOOP in ksh

hi i want to scp files from remote server B to my local server A... and i have a file containing list of all files to be scped from remote server B there is a passwordless connectivity set between remote server and my local server. need a ksh script.. with a for loop that goes through... (2 Replies)
Discussion started by: billpeter3010
2 Replies

5. UNIX for Advanced & Expert Users

Remove first line from mutiple files

How to remove the first line from multiple files and use it as source to the jobs. Only at the runtime it should remove the first line not in the file . (1 Reply)
Discussion started by: etldeveloper
1 Replies

6. Shell Programming and Scripting

Some manipulations with files and folders. (loop, find, create and remove)

Hello! I need to realize such task. 1. In my user's home dir I have folder1; 2. In folder1 I have some (various count) subfolders with random names; 3. In these subfolders I have one file anyname.pdf (various name in each subfolder) and file content.txt (constant name in each subfolder) ##... (7 Replies)
Discussion started by: optik77
7 Replies

7. Shell Programming and Scripting

remove a line in files

Hi I have 3 files and I want to remove 1 line in each file. This line correposnds to the occurrence of a specific pattern any idea how to do this in shell? thx (4 Replies)
Discussion started by: melanie_pfefer
4 Replies

8. Shell Programming and Scripting

Remove '^M' in each line of files

Hi All, Working on AIX 5.3 we need to remove '^M' in each line of files. could anyone please share such an experience would be appreciated. Thanks for your time! Regards, (9 Replies)
Discussion started by: a1_win
9 Replies

9. UNIX for Dummies Questions & Answers

Ksh Storing Multiple Files and reading each line in each file.

How would I go about storing multiple file paths in a directory that begin like: 20080402* and run a loop that reads each line of each file thats in a given directory. So far this is what I have: #!/bin/ksh echo "ENTER Reprint Date (YYYYMMDD): " read ReprintDate echo ""... (1 Reply)
Discussion started by: developncode
1 Replies

10. Shell Programming and Scripting

remove the first line of all files

I want to remove the first line of all files in a directory with .dat extension. Can any one help me on this with a small script. I want the file names to remain same . (8 Replies)
Discussion started by: dineshr85
8 Replies
Login or Register to Ask a Question