-1

I am trying to convert the output of

ps awux

into 11 fields with the headers eliminated and then importing that output into an existing table in SQL.

0

2 Answers 2

0
for line in $(ps auwx | sed -e 's/\ \+/\",\"/g'); do
    echo "insert into tbl_name values(\"$line\")"; 
done | mysql
3
  • have to be more careful when the command column contains whitespace Commented Nov 10, 2015 at 18:56
  • @glennjackman Yup...that would break things, wouldn't it. Short of a messy awk script you're probably going to have to break this out into a real scripting language. Commented Nov 10, 2015 at 19:01
  • Also, don't read lines with "for": mywiki.wooledge.org/DontReadLinesWithFor Commented Nov 10, 2015 at 19:06
0

You can remove the first line with sed 1d

You can create the values list with

sql=$( 
    echo "$line" | sed "
    s/'/''/g
    s/[[:space:]]\+/','/   # 1
    s/[[:space:]]\+/','/   # 2
    s/[[:space:]]\+/','/   # 3
    s/[[:space:]]\+/','/   # 4
    s/[[:space:]]\+/','/   # 5
    s/[[:space:]]\+/','/   # 6
    s/[[:space:]]\+/','/   # 7
    s/[[:space:]]\+/','/   # 8
    s/[[:space:]]\+/','/   # 9
    s/[[:space:]]\+/','/   # 10
    s/^/INSERT INTO table_name VALUES ('/
    s/$/');/
    "
)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.