LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-24-2016, 01:56 PM   #1
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Rep: Reputation: 69
add unique string using braces to several files


I guess the meaning of the title is far from obvious, but I wasn't able to come up with a better one.

So this is only for practice purposes.

Given several files under a directory, I'm trying to add some string, text, whatever to each one, so that each string is unique. For instance, file1 should contain the text "filea", file2, the text "fileb" and so on.

What I tried obviously didn't work:
Code:
for var_1 in $(ls .); do echo file{a..f} > $var_1; done
All files have identical content
 
Old 07-24-2016, 02:15 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Not really sure why you thought this would yield different data when all the work is being done inside the loop.

Ultimately the issue I see here is that you would need to know ahead of time how many files you have in the directory? reason being is that your current example, if it worked, would only do so for 6 or less
files as the moment you encounter a seventh file you would be back to 'filea' and hence no unique string.

So you may need to provide more information on what you know prior to running the script before we can help you further??

I will add that the use of ls in a for loop is an enormous no no ... use the simple glob metacharacter as it will not have any of the word splitting issues of your current example:
Code:
for var_1 in *
 
1 members found this post helpful.
Old 07-24-2016, 02:23 PM   #3
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Original Poster
Rep: Reputation: 69
There's a random number of files in a directory. I don't need a particular number. Let's say 10, if it's essential to solving the problem.
I'm simply experimenting, I'm trying to add succesive strings of the {a..z} (braces) type to the content of these 10 files. Ok, I understand that if it's 10 files, then I'd need something like {a..j}. So how do I add "a" to "file1", "b" to "file2" and so on, and so forth?

I don't need the answer, I need to know how to approach the problem. The solution is obviously very different from what I've tried, but I've no clue whatsoever.
 
Old 07-24-2016, 02:40 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,544

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
I would place your alphabet in an array which can then be echoed to each file and an increment made to the counter. Again this will only work for 26 files, so an additional part of your code might
need to be a count of just how many files there are so you know ahead of time if the answer will be unique or not.
 
Old 07-24-2016, 03:02 PM   #5
vincix
Member
 
Registered: Feb 2011
Distribution: Centos 6.7, 7
Posts: 689

Original Poster
Rep: Reputation: 69
Well, I'm still not very familiar with arrays, so I'm going to have to read a little bit more before going any further. Thanks.
 
  


Reply

Tags
bash, for loop, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Search Directory full of txt files for a unique string imkornhulio Programming 8 12-13-2013 10:23 AM
generating unique integer from string niteen Linux - Newbie 3 03-28-2013 06:05 AM
How to unique the content within a string with seperator? yuluilui Programming 3 05-18-2011 06:40 PM
Detect Unique System Identification string kushalkoolwal Programming 18 07-03-2008 01:30 PM
creating a unique id string in shell gumby Programming 4 05-07-2003 05:56 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration