Reese Knowledgebase

concatenate a file list into a string

View Kristian Reese's profile on LinkedIn


If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 60
by: Reese K.
Posted: 17 Aug, 2012
Last updated: 19 Jul, 2013
Views: 1396

Example 1

kreese@myvps:arp $ cat ips
205.178.186.72
205.178.136.60
205.178.136.72
205.178.136.91
205.178.136.109
205.178.136.132
205.178.136.166
205.178.186.206
205.178.186.220
206.188.212.32
206.188.212.142
206.188.212.148
206.188.212.163
206.188.212.164
206.188.212.165
206.188.212.166
206.188.212.167
206.188.212.168
206.188.212.169
206.188.212.170
206.188.212.171
206.188.212.193
69.161.223.12
69.161.223.104
69.161.223.236
69.161.223.237
206.188.208.186
206.188.207.1
206.188.207.209
206.188.207.254
206.188.206.50
206.188.206.60
206.188.206.178
206.188.206.180
206.188.204.129
206.188.204.254
kreese@myvps:arp $
kreese@myvps:arp $ ips=$(< ./ips awk '$1')
kreese@myvps:arp $ echo $ips
205.178.186.72 205.178.136.60 205.178.136.72 205.178.136.91 205.178.136.109 205.178.136.132 205.178.136.166 205.178.186.206 205.178.186.220 206.188.212.32 206.188.212.142 206.188.212.148 206.188.212.163 206.188.212.164 206.188.212.165 206.188.212.166 206.188.212.167 206.188.212.168 206.188.212.169 206.188.212.170 206.188.212.171 206.188.212.193 69.161.223.12 69.161.223.104 69.161.223.236 69.161.223.237 206.188.208.186 206.188.207.1 206.188.207.209 206.188.207.254 206.188.206.50 206.188.206.60 206.188.206.178 206.188.206.180 206.188.204.129 206.188.204.254

Example 2:

nodevs=$(< /proc/filesystems awk '$1 == "nodev" { print $2 }')

Example 3:

One of my favorites is when I need to kill off a bunch of processes belonging to a specific user.  In comes in really handy when a customers VPS is hacked and they've created a privlidged user account where you want to quickly kill of a bunch of pids belonging to that user:

kill -9 $(ps -U 10000 | awk '{ print $1 }' | grep -v PID)

or use

ps -U apache -o pid --no-headers

Example 4:

Using tr

tr -s '\n' ' ' < file.txt

Example 5:

[root@vps11 ~]# for veid in $(cat vps5.migrations  | awk '{ print $4 }'); do echo -ne "$veid,"; done
7879,7868,11370,7843,8493,12116,13739,7915,7954,8640,[root@vps11 ~]#

This article was:   Helpful | Not Helpful
Prev   Next
Script for Log File Maintenance     crontab format

RSS