Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have two files which has exact same number of lines.
I want first line of first file should be filename of new file and content of this new file should be first line of second file.
Then second line of first file should be filename of again new file and content of this new file should be second line of second file.
then third line of first file should be filename of again new file and content of this new file should be third line of second file.
and so on...
I am trying to do it using for loop but I am not able to create two for loops.
This is what I have done
Code:
IFS=$'\n'
var=$(sed 's/\"http\(.*\)\/\(.*\).wav\"\,\".*/\2/g' 1797.csv) # filenames of all files
var2=$(sed 's/\"http\(.*\)\/\(.*\).wav\"\,\"\(.*\)\"$/\3/g' 1797.csv) # contents of all files
for j in $var;
do
#Here I do not know how to use $var2
done
Please help.
Last edited by unclesamcrazy; 03-18-2015 at 05:25 AM.
I created separate two files to use in BASH script but I was not able to do it either using shell script so I scraped the procedure.
Originally, there is only one file 1797.csv which has almost 100000 lines like above. There are two columns. One is url and other is description.
Every url contains 10 digit .wav filename.
I need to create individual text files whose filename will be filename of wav file and whose content will be description of that .wav file.
and in your script you will have three variables, the filename, url and description. It looks really easy to implement, but I would like to ask you to show us a few lines (as example, with fake data probably) just to better understand your needs.
Too much tried in awk, not able to write it.
I am using for loop but not able to succeed, here is my for loop script.
Quote:
IFS="
"
var=$(sed 's/\"http\(.*\)\/\(.*\).wav\"\,\".*/\2/g' 1797.csv)
for i in $var
do
lst=$(sed 's/\"http\(.*\)\/\(.*\).wav\"\,\"\(.*\)\"$/\3/g' 1797.csv)
IFS="
"
for j in $lst
do
echo $i
done
echo $j
done
I am trying to print, first stored value of variable $var and first stored value of variable $lst then
second stored value of variable $var and second stored value of variable $lst
but it prints all values of variable $lst then all values of variable $var.
Please help to print first from each then second from each then third and so on...
You need to look at the examples provided again. We are not using gsub but rather gensub. gsub returns the number of changes made whereas gensub returns the altered string, which is what you want.
Thank you grail for your explanation.
awk is a beauty. I always wanted to learn awk but it is really tough.
I have tried using bash array, I don't know why I was using two loops, I have tried to do with single loop. It is working(for now).
Quote:
#!/bin/bash
IFS=$'\n'
ARR=( $(cat 1797.csv) )
for i in "${ARR[@]}"
do
var=$(echo $i | sed 's/\"http\(.*\)\/\(.*\).wav\"\,\".*/\2/g')
var2=$(echo $i | sed 's/\"http\(.*\)\/\(.*\).wav\"\,\"\(.*\)\"$/\3/g')
echo $var2 > $var.txt
done
But your solution is sweet, it is portable. I wish I could write awk one liners.
Can you please help me to do this task using awk array (one liner)?
Thanks
No your scripts have done their job successfully. i was trying to learn by doing same task with different methods.
I want to learn awk but tutorials are quite tough which are suggested by experts here.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.