Read file(s) and SFTP it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read file(s) and SFTP it
# 1  
Old 01-25-2010
Read file(s) and SFTP it

I have files starting with some char 'X' (they could vary from day to day) and I have to read all the files starting with char X*.txt and write to a single file Y.txt. But I have to omit the first line in each file (header) and consider only the header in first file i read.

After all the writing is done to Y.txt I should sftp it to a server.

Appreciate your help regarding this.

Thanks.
# 2  
Old 01-25-2010
hello ,

Please rephrase this - "But I have to omit the first line in each file (header) and consider only the header in first file i read."
# 3  
Old 01-25-2010
If the number of files is small enough to glob properly, you can do

Code:
tail -q -n +2 X*.txt > Y.txt

To get the header of the first file:
Code:
for FILE in X*.txt
do
        read LINE < $FILE
        echo "$LINE" > Y.txt
        break
done

tail -q -n +2 X*.txt >> Y.txt

..presuming the number of files doesn't change between the first execution and the second.

Last edited by Corona688; 01-25-2010 at 12:28 PM.. Reason: add -q to prevent garbage in output!
# 4  
Old 01-25-2010
Lets say the files are like this

File Name: X1.txt

header1st
abc
bcd
age

File Name: X2.txt

header1st
abcege
ageerw
ageaer

.......

in my result file Y.txt

I should have only

header1st
abc
bcd
age
abcege
ageerw
ageaer

I don't need to add all the headers of every file I read, I need only the first file (which ever I read first) header, since all the headers are going to be the same.
# 5  
Old 01-25-2010
Here's a way that won't mess up when the number of files changes.

Code:
# Open FD 5 for writing to Y.txt
exec 5>Y.txt

TMP=$(mktemp)
ls X*.txt > ${TMP}
# Open FD 6 for reading from the temp file
exec 6<${TMP}

read FIRST <&6
cat "${FIRST}" >&5

xargs tail -q -n +2 <&6 >&5

# close streams
exec 5>&-
exec 6>&-

This is untested.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sftp - Couldn't read packet: Connection reset by peer

Hello to all, i have a problem when trying to estabilish a sftp connection. i setup a sftp server (i used feeFTPd) and i'm now trying to connect from two different machines. from the first one everything is fine: $ sftp -vvv user@xxx.xxx.xxx.xxx Connecting to xxx.xxx.xxx.xxx...... (9 Replies)
Discussion started by: dc26
9 Replies

2. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

3. Shell Programming and Scripting

How to overwrite a file by transferring the file using sftp?

hi, i am using sftp to transfer files between two servers, if i connect to a remote server using sftp n moves a file say "S123.txt" to the remote server, it moves successfully, but when i try to execute the same sftp script to move the same files to the same remote server, the old file is... (3 Replies)
Discussion started by: Little
3 Replies

4. Shell Programming and Scripting

Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements . #!/bin/sh echo "Starting to sftp..." sftp admin@myip << END_SCRIPT cd /remotepath/ lcd /localpath/ mget myfiles*.csv bye END_SCRIPT echo "Sftp successfully." echo echo... (11 Replies)
Discussion started by: scriptscript
11 Replies

5. Red Hat

Chroot sftp users, remote sftp login shows wrong timestamp on files

Hello, I have a weird issue, I have RHEL 5.7 running with openssh5.2 where sftpgroup OS group is chroot. I see the difference difference in timestamp on files, when I login via ssh and SFTP, I see four hour difference, is something missing in my configuration. #pwd... (8 Replies)
Discussion started by: bobby320
8 Replies

6. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

7. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

8. Shell Programming and Scripting

sftp shell script - Password read error.

Hi, I have script which does the sftp function. In the script in one place it able to read the password from file and other place files with below error. warning: has much more security than supplying the password in the clear warning: on the command line. Failed to get password: File... (0 Replies)
Discussion started by: vino_hymi
0 Replies

9. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

10. Shell Programming and Scripting

sftp -b doesn't read the batchfile

Several of our end-users need to send a file to our insurance carrier using ssh and sftp. We've put together a Windows VBS script that opens the ssh tunnel and calls sftp with the -b option pointing to a batch script in the same directory, however sftp doesn't seems to be reading from the... (1 Reply)
Discussion started by: kmw
1 Replies
Login or Register to Ask a Question