1

I have in a folder (eg /tmp) the following files

1.id
2.id
3.id
4.id

so on...

In these files, there is one number inside. For example in 1.id it can be the 1000, in 2.id it can be the 2000 etc.

I want an one line bash command to get the value (number) of all these files automatically (*.id) but append also the filename of it.

So the output should be:

1.id=1000
2.id=2000
  • How about awk instead of bash? awk '{$0 = FILENAME"="$0}1' *.id – steeldriver Aug 12 '15 at 15:49
3

Just use grep in this folder:

grep "" *.id

Output:

1.id:123
2.id:13
3.id:5
4.id:87876

BTW: I often use this in proc or sysfs filesystems;

cd /sys/class/net/eth0
grep "" *

This gives you all infos in sysfs about the ethernet interface eth0.

  • Well that works and it is awesome, however if one file exists eg 1.id it doesnt show the name of it – OhGodWhy Aug 12 '15 at 18:06
  • @BlackDream Just use -H and it will print a filename even if there is only one file. – chaos Aug 12 '15 at 18:08
1

Using awk

awk '{printf "%s=%s\n",FILENAME,$0}' *.id

or

awk 'OFS="=" {print FILENAME,$0}' *.id

Examples

% awk '{printf "%s=%s\n",FILENAME,$0}' *.id
1.id=1000
2.id=2000

% awk 'OFS="=" {print FILENAME,$0}' *.id
1.id=1000
2.id=2000

% awk '{printf "%s:%s\n",FILENAME,$0}' *.id
1.id:1000
2.id:2000

and so on

0

Here is a bash one liner :

for i in *.id; do echo ""$i"=$(<"$i")"; done

Example :

$ ls -1
1.id
2.id


$ for i in *.id; do echo ""$i"=$(<"$i")"; done
1.id=10000
2.id=2000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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