【问题标题】:SED formatting assistance [closed]SED formatting assistance [closed]
【发布时间】:2022-12-27 05:39:25
【问题描述】:

I have the following data, I'm trying to:

  • Remove all occurrences of:
    1. pveEnabled,
    2. exclude
    3. and random :'s
    4. Order /organize the numbers so they're in the format: "1503287189", with the final entry having no comma so the JSON is valid.

ORIGINAL:

pvpEnabled": "pvpEnabled", "1503287189": "pvpEnabled", "1503361188": "pvpEnabled", "1503376934": "exclude", "1503381175": "pvpEnabled", "1503387015": "pvpEnabled", "1503391493": "exclude", "1503396497": "exclude", "1503401501": "pvpEnabled", "1503406506": "exclude", "1503411510": "exclude", "1503416515": "exclude", "1503421519": "exclude", "1503426519": "exclude", "1503431524": "exclude", "1503438027": "exclude", "1503442663": "pvpEnabled", "1503447963": "pvpEnabled", "0148370988": "pvpEnabled", "0148365973": "pvpEnabled", "0148360804": "exclude", "0148355806": "exclude", "0148350792": "exclude", "0148345788": "exclude", "0148340791": "exclude", "0148335783": "pvpEnabled", "0148330776": "exclude", "0148325719": "exclude", "0148320988": "exclude", "0148313835": "pvpEnabled", "0148295030": "pvpEnabled", "0148288501": "pvpEnabled", "0148215062": "pvpEnabled", "1345579563": "pvpEnabled", "1345574580": "pvpEnabled", "1345569326": "exclude", "1345564323": "exclude", "1345559315": "exclude", "1345554331": "exclude", "1345549301": "exclude", "1345544302": "pvpEnabled", "1345539298": "exclude", "1345534286": "exclude", "1345528884": "exclude", "1345523882": "pvpEnabled", "1345505015": "pvpEnabled", "1345498995": "pvpEnabled", "1345427050": "pvpEnabled", "0602409122": "pvpEnabled", "0602484983": "pvpEnabled", "0602490896": "pvpEnabled", "0602508375": "pvpEnabled", "0602515338": "exclude", "0602519942": "exclude", "0602524947": "exclude", "0602529950": "pvpEnabled", "0602534951": "exclude", "0602539955": "exclude", "0602544961": "exclude", "0602549962": "exclude", "0602555019": "exclude", "0602560012": "pvpEnabled", "0602565022": "pvpEnabled", "0603066190": "pvpEnabled", "0603139249": "pvpEnabled", "0603145085": "pvpEnabled", "0603162967": "pvpEnabled", "0603167968": "exclude", "0603173326": "exclude", "0603178332": "exclude", "0603183336": "pvpEnabled", "0603188342": "exclude", "0603193344": "exclude", "0603198348": "exclude", "0603203367": "exclude", "0603208353": "exclude", "0603213359": "pvpEnabled", "0603218362": "pvpEnabled", "0602343883": "pvpEnabled", "0602415622": "pvpEnabled", "0602421320": "pvpEnabled", "0602439549": "pvpEnabled", "0602446549": "exclude", "0602451217": "exclude", "0602456224": "exclude", "0602461226": "pvpEnabled", "0602466229": "exclude", "0602471252": "exclude", "0602476254": "exclude", "0602481256": "exclude", "0602486258": "exclude", "0602491263": "pvpEnabled", "0602496275": "pvpEnabled", "0602358904": "pvpEnabled", "0602431268": "pvpEnabled", "0602437049": "pvpEnabled", "0602455763": "pvpEnabled", "0602462736": "exclude", "0602467460": "exclude", "0602472469": "exclude", "0602477472": "pvpEnabled", "0602482497": "exclude", "0602487499": "exclude", "0602492561": "exclude", "0602497677": "exclude", "0602502734": "exclude", "0602507684": "pvpEnabled", "0602512736": "pvpEnabled", "0836232343": "pvpEnabled", "0836304026": "pvpEnabled", "0836309768": "exclude", "0836327849": "pvpEnabled", "0836332844": "pvpEnabled", "0836337847": "exclude", "0836342847": "exclude", "0836347868": "pvpEnabled", "0836352890": "exclude", "0836357885": "exclude", "0836362929": "exclude", "0836367934": "exclude", "0836372938": "exclude", "0836377940": "exclude", "0836383068": "exclude", "0836388085": "pvpEnabled", "0836393085": "pvpEnabled", "0043143450": "pvpEnabled", "0043138446": "pvpEnabled", "0043133446": "exclude", "0043128443": "exclude", "0043123441": "exclude", "0043118441": "exclude", "0043113430": "exclude", "0043108429": "pvpEnabled", "0043103428": "exclude", "0043098422": "exclude", "0043093907": "pvpEnabled", "0043087990": "pvpEnabled", "0043083584": "exclude", "0043068763": "pvpEnabled", "0043002408": "pvpEnabled", "0602497036": "pvpEnabled", "0602558084": "pvpEnabled", "0602575079": "exclude", "0602581727": "pvpEnabled", "0602586927": "pvpEnabled", "0602591697": "exclude", "0602596698": "exclude", "0603001701": "pvpEnabled", "0603006704": "exclude", "0603011708": "exclude", "0603017336": "exclude", "0603022094": "exclude", "0603027096": "exclude", "0603032100": "pvpEnabled", "0603037103": "pvpEnabled", "1533021993": "pvpEnabled", "1533016952": "pvpEnabled", "1533011941": "exclude", "1533006936": "exclude", "1533001936": "exclude", "1532596889": "exclude", "1532591889": "exclude", "1532586887": "pvpEnabled", "1532581881": "exclude", "1532576870": "exclude", "1532572051": "pvpEnabled", "1532566829": "pvpEnabled", "1532561663": "exclude", "1532546376": "pvpEnabled", "1532480261": "pvpEnabled"

REQUESTED OUTPUT:

"1532591889",
"1532586887",
"1532581881",
"1532576870",
"1532572051",
"1532566829",
"1532561663",
"1532546376",
"1532480261"

Thanks!

I hacked together this but I'm out of my depths:

sed 's/: "pvpEnabled"\|"exclude"\|://g'

【问题讨论】:

  • are you saying those ~170 input pairs should generate only 9 output lines? if not, please insure the sample input and expected outputmatch(preference would be to reduce the sample input to something quite a bit smaller ... we only need enough to demonstrate matches and misses)
  • when you say to remove 'exclude' do you meanjustthe "exclude" entries or do you also want to remove the associated numeric value, too? regardless, I don't understand how you can have an input of "1532596889": "exclude", "1532591889": "exclude", and only 1532591889 shows up in the output, ie, why isn't 1532596889 also in the output ????
  • no reason to close or downvote this, mods are sour...

标签: linux bash unix sed sh


【解决方案1】:

I think this does it. I used two for loops. The first stores the numbers in an array, the second prints them.

#!/usr/bin/env bash

IFS=" " read -r -a WORDS <<< "$(tr '
' ' ' < in.txt)"

declare -a MATCH=()  # declare empty arry to store matches

# true to skip
for (( i=0; i < ${#WORDS[@]}; i++ ))
do
    if [[ "${WORDS[i]}" =~ "pvpEnabled" ]]; then
        true
    elif [[ "${WORDS[i]}" =~ "exclude" ]]; then
        true
    else
        WORD=${WORDS[i]}
        MATCH+=(${WORD%:}) # append to array with removal of suffix pattern
    fi
done

for (( i=0; i < ${#MATCH[@]}-1; i++ ))
do
    echo "${MATCH[i]}",
done
echo "${MATCH[i]}"

【讨论】:

  • The script assumes the input is captured to in.txt
  • good to see some help from the community despite the negativity (votes, closed questions).. thank you!
【解决方案2】:

This might work for you (GNU sed):

sed -E 's/"([0-9]+)"/
,
/g;s/,
[^
]*$/
/;/^[0-9]/P;D' file

For all double quoted integers, replace the double quotes by newlines and append a comma to the integer.

Remove the last comma inserted.

Print the first line it if it begins with an integer and then delete the first line.

Repeat until all lines are consumed.

【讨论】:

  • good to see some help from the community despite the negativity (votes, closed questions).. thank you!
猜你喜欢
  • 1970-01-01
  • 2017-11-02
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2014-11-22
相关资源
最近更新 更多