Processing files one by one using data from pipe


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Processing files one by one using data from pipe
# 1  
Old 11-11-2017
Processing files one by one using data from pipe

Hi guys, I receive a list from pipe (with fixed number of lines) like this:
Code:
name1
name2
name3

And in my ./ folder I have three files:
Code:
01-oldname.test
02-someoldname.test
03-evenoldername.test

How to rename files one by one using while read?
Desired result:
Code:
01-name1.test
02-name2.test
03-name3.test

I guess construction should look like this:
Code:
cat ../names.txt | while read -r line; do mv



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-12-2017 at 08:08 AM.. Reason: Added CODE tags.
# 2  
Old 11-11-2017
One approach is to load names into an indexed array and then use another loop to rename the files using the array:-
Code:
#!/bin/ksh

# Load names into an indexed array
c=1
while read file
do
        A_fname[$c]="$file"
        (( ++c ))
done < names.txt

# Rename *.test files using array value
c=1
for file in *.test
do
        if [ ! -z ${A_fname[$c]} ]
        then
                print "mv ${file} ${file%%-*}-${A_fname[$c]}.test"
                (( ++c ))
        fi
done

Remove the highlighted print and rerun if the output looks good.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-12-2017
Try also
Code:
ls *.test | while read FN && read NFN <&3; do echo mv $FN ${FN%-*}-$NFN.${FN#*.}; done 3<names.txt
mv 01-oldname.test 01-name1.test
mv 02-someoldname.test 02-name2.test
mv 03-evenoldername.test 03-name3.test

remove echo if happy with result.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-12-2017
Another variation:
Code:
for i in *.test; do
  read new || break
  mv "$i" "${i%%-*}-${new}.${i##*.}"
done <names.txt

These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data Processing

I have below Data *************************************************** ********************BEGINNING-1******************** directive url is : https://coursera-eu.mokar.com/directives/96df29ff-176a-35f7-8b1b-4ce483d15762 Src urls are :... (8 Replies)
Discussion started by: nikhil jain
8 Replies

2. Shell Programming and Scripting

Processing a file list via named pipe

I have a ksh93 script I use that processes a file list in the order that they exist in the list. I would like to speed up processing of the list by having multiple processes handle it at once. I was thinking that perhaps a good way to handle this would be to write the list to a named pipe and some... (4 Replies)
Discussion started by: benalt
4 Replies

3. Shell Programming and Scripting

Data processing using awk

Hello, I have some bitrate data in a csv which is in an odd format and is difficult to process in Excel when I have thousands of rows. Therefore, I was thinking of doing this in bash and using awk as the primary application except that due to its complication, I'm a little stuck. ... (24 Replies)
Discussion started by: shadyuk
24 Replies

4. Shell Programming and Scripting

Read pipe data

Hello, I need to read the pipe data as:- cat abc.txt | uuencode abc.txt | mailx -s hi xyz@xyz.com I will override the mailx function so that when mailx is called, it calls my version of maix and in that function I want to read the file which is attached in progional mailx function- abc.txt... (7 Replies)
Discussion started by: shubh05
7 Replies

5. UNIX for Dummies Questions & Answers

Genomic data processing

Dear fellow members, I've just joined the forum and am a newbie to shell scripting and programming. I'm stuck on the following problem. I'm working with large scale genomic data and need to do some analyses on it. Essentially it is text processing problem, so please don't mind the scientific... (0 Replies)
Discussion started by: mvaishnav
0 Replies

6. Programming

Data processing

Hello guys! I have some issue in how to processing some data. I have some files with 3 columns. The 1st column is a name of my sample. The 2nd column is a numerical sequence (very big sequence) starting from "1". And the 3rd column is a feature of each line, represented for a number (completely... (2 Replies)
Discussion started by: bfantinatti
2 Replies

7. Shell Programming and Scripting

awk script processing data from 2 files

Hi! I have 2 files containing data that I need to process at the same time, I have problems in reading a different number of lines from the different files. Here is an explanation of what I need to do (possibly with an awk script). File "samples.txt" contains data in the format: time_instant... (6 Replies)
Discussion started by: Alice236
6 Replies

8. Shell Programming and Scripting

Help with data processing, maybe awk

I have a file, first 5 columns are very normal, like "1107",106027,71400,"Y","BIOLOGY",, however, the 6th columns, the user can put comments, anything, just any characters, like new line, double quote, single quote, whatever from the keyboard, like"Please load my previous SOM597G course content in... (3 Replies)
Discussion started by: freelong
3 Replies

9. Shell Programming and Scripting

How should i know that the process is still processing data

I have some process . How should i know that the process is still processing data or got hanged even though it is showing that it is running in background I know of a command called truss. how should i use this command and determine 1) process is still processing data 2) process got hanged... (7 Replies)
Discussion started by: ali560045
7 Replies

10. UNIX for Advanced & Expert Users

data processing

hi i am having a file of following kind: 20015#67143645#143123#4214 62014#67143148#67143159#456 15432#67143568#00143862#4632 54112#67143752#0067143657#143 54623#67143357#167215#34531 65446#67143785#143598#7456 75642#67143546#156146#845 24464#67143465#172532#6544... (5 Replies)
Discussion started by: rochitsharma
5 Replies
Login or Register to Ask a Question