1

I want to list all of my files in a folder to a txt file but at the same time I need to write a string next to them such as:

aaa.txt should be in the form of:

/home/user/a1.jpg 1
/home/user/a2.jpg 1
/home/user/a3.jpg 1
/home/user/a4.jpg 1

I can get the first one with

find `pwd` | cat > aaa.txt

However, I cannot write "1" next to each line. There are millions of lines so I cannot do it manually, too. Is there any way to do this?

By the way, I'm using Ubuntu and if it's possible it needs to be done by bash commands.

3 Answers 3

2

AWK

I used awk for this

find `pwd` | awk -F\, '{print $1 " 1"}'

AWK is a superb tool for all sorts of report generation tasks. Along with sed, it is one my core UNIX/Linux tools. Sed lacks the support for loops, arrays and conditionals so AWK is the more powerful of the two.

You can guess how that works, the -F sets the field separator (to avoid spaces), and there are lots of good resources on-line for AWK. Try this one here. Yes the syntax is a little odd at first but it really is worth making the effort to learn the basics.


Sed

As pointed out, you can solve the problem using sed and for this example it may well be the simpler and better solution but it sed is limited in its capabilities. There's a great comparison of the two here on Stackoverflow.

2
  • File names do not have spaces in my case. This actually solves for my problem ` find pwd | awk '{print $1 " 1"}' | cat > training.txt` Commented Mar 22, 2016 at 12:04
  • 1
    You can use the -F option to set the field separator to whatever you need Commented Mar 22, 2016 at 12:14
1

AWK will not work with just printing $1 if file names have spaces in them.

Use SED instead.

find `pwd` | sed 's/$/ 1/' > aaa.txt

Sed gives you a lot more manipulation with search and replacements if needed.

2
  • 1
    @cagatayodabasi, with this way you can also add numbers as needed in start of lines as well and also write numbers sequentially. Let me know if you need sed scripts for those.
    – MohitC
    Commented Mar 22, 2016 at 12:11
  • AWK is the more generic solution as it comes with a full programming language focused on text manipulation but if sed works for this task then go for it! Reference to AWK v sed is stackoverflow.com/questions/1632113/… Commented Mar 22, 2016 at 12:17
0

It's easy. Once aaa.txt is completed as :

/home/user/a1.jpg
/home/user/a2.jpg
/home/user/a3.jpg
/home/user/a4.jpg

Find for new-spaces in the aaa.txt using normal search syntax. Replace the new-spaces with whatever string you want to append including new-space. It's working for me correctly. Try it and reply if not working :)

2
  • It can work, but I need to automate the task rather than searching for some strings. You can check the other answers gives that work and automate the task. Commented Mar 22, 2016 at 12:15
  • @cagatayodabasi Okay :)
    – Raj sree
    Commented Mar 29, 2016 at 9:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.