Parse data based of number of digits in quotesBash script to edit text in csv-formatFind matches from an...
Is there an evolutionary advantage to having two heads?
Can a wire having a 610-670 THz (frequency of blue light) AC frequency supply, generate blue light?
What caused the tendency for conservatives to not support climate change regulations?
Modern approach to radio buttons
How did early x86 BIOS programmers manage to program full blown TUIs given very few bytes of ROM/EPROM?
How crucial is a waifu game storyline?
Do creatures all have the same statistics upon being reanimated via the Animate Dead spell?
How to capture more stars?
What is the intuition behind uniform continuity?
Can't connect to Internet in bash using Mac OS
Why would Lupin kill Pettigrew?
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
What's the most polite way to tell a manager "shut up and let me work"?
The qvolume of an integer
Adding strings in lists together
The deliberate use of misleading terminology
What is the indigenous Russian word for a wild boar?
What does "tea juice" mean in this context?
How to properly maintain eye contact with people that have distinctive facial features?
Why do Russians call their women expensive ("дорогая")?
Possible nonclassical ion from a bicyclic system
What is the 中 in ダウンロード中?
chmod would set file permission to 000 no matter what permission i try to set
How to detach yourself from a character you're going to kill?
Parse data based of number of digits in quotes
Bash script to edit text in csv-formatFind matches from an index file without exact matching and print the last fieldGrep / awk on multiple files to single outputhalve number fields above 10000, dropping decimalsDivide numbers in specific fieldHow to split a huge line of characters into several lines with equal numbers of columns?Count numbers present in each line for unique String using shell scriptText processing - How to get multiple patterns in order from a filealter awk variable based on match inside awkOrdering a string by the count of substrings?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I have a large amount of data currently in the current format:
a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}
The numbers inside "" before them have s:4. For numbers that are 3 digits long this should be changed to s:3 and digits that are 5 long should be s:5 and so on.
The converted data should look like this:
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
Each data string {} is on it's own line in data.txt
text-processing
add a comment |
I have a large amount of data currently in the current format:
a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}
The numbers inside "" before them have s:4. For numbers that are 3 digits long this should be changed to s:3 and digits that are 5 long should be s:5 and so on.
The converted data should look like this:
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
Each data string {} is on it's own line in data.txt
text-processing
add a comment |
I have a large amount of data currently in the current format:
a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}
The numbers inside "" before them have s:4. For numbers that are 3 digits long this should be changed to s:3 and digits that are 5 long should be s:5 and so on.
The converted data should look like this:
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
Each data string {} is on it's own line in data.txt
text-processing
I have a large amount of data currently in the current format:
a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}
The numbers inside "" before them have s:4. For numbers that are 3 digits long this should be changed to s:3 and digits that are 5 long should be s:5 and so on.
The converted data should look like this:
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
Each data string {} is on it's own line in data.txt
text-processing
text-processing
asked 2 hours ago
Teddy291Teddy291
99931525
99931525
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
#!/usr/bin/env bash
IFS=';'
while read LINE
do
set -- $LINE
while [ "$1" ]
do
if grep -qEx 's:[0-9]+:".*"' <<< "$1"; then
s=${1##*:}
printf 's:%d:%s%s' $((${#s}-2)) "$s" "$IFS"
else
printf '%s%s' "$1" "$IFS"
fi
shift
done
printf 'n'
done < data.txt
This script sets the field separator to the semicolon character, and then iterates through the lines of data.txt, splitting each line into separate fields on the semicolon delimiter. For fields that begin with s:###:"..." (for arbitrary values of ### and ...), the script calculates the length of the quoted string, and reformats the field using that length value and adding a trailing field separator. Fields which do not begin with s:###:"..." are output verbatim, again adding back the trailing field separator.
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";};
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either[[orcase.
– icarus
42 mins ago
add a comment |
How about
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
Ex.
$ echo 'a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}' |
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
You can add -i to perform the substitution on the file in-place.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f521636%2fparse-data-based-of-number-of-digits-in-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
#!/usr/bin/env bash
IFS=';'
while read LINE
do
set -- $LINE
while [ "$1" ]
do
if grep -qEx 's:[0-9]+:".*"' <<< "$1"; then
s=${1##*:}
printf 's:%d:%s%s' $((${#s}-2)) "$s" "$IFS"
else
printf '%s%s' "$1" "$IFS"
fi
shift
done
printf 'n'
done < data.txt
This script sets the field separator to the semicolon character, and then iterates through the lines of data.txt, splitting each line into separate fields on the semicolon delimiter. For fields that begin with s:###:"..." (for arbitrary values of ### and ...), the script calculates the length of the quoted string, and reformats the field using that length value and adding a trailing field separator. Fields which do not begin with s:###:"..." are output verbatim, again adding back the trailing field separator.
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";};
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either[[orcase.
– icarus
42 mins ago
add a comment |
#!/usr/bin/env bash
IFS=';'
while read LINE
do
set -- $LINE
while [ "$1" ]
do
if grep -qEx 's:[0-9]+:".*"' <<< "$1"; then
s=${1##*:}
printf 's:%d:%s%s' $((${#s}-2)) "$s" "$IFS"
else
printf '%s%s' "$1" "$IFS"
fi
shift
done
printf 'n'
done < data.txt
This script sets the field separator to the semicolon character, and then iterates through the lines of data.txt, splitting each line into separate fields on the semicolon delimiter. For fields that begin with s:###:"..." (for arbitrary values of ### and ...), the script calculates the length of the quoted string, and reformats the field using that length value and adding a trailing field separator. Fields which do not begin with s:###:"..." are output verbatim, again adding back the trailing field separator.
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";};
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either[[orcase.
– icarus
42 mins ago
add a comment |
#!/usr/bin/env bash
IFS=';'
while read LINE
do
set -- $LINE
while [ "$1" ]
do
if grep -qEx 's:[0-9]+:".*"' <<< "$1"; then
s=${1##*:}
printf 's:%d:%s%s' $((${#s}-2)) "$s" "$IFS"
else
printf '%s%s' "$1" "$IFS"
fi
shift
done
printf 'n'
done < data.txt
This script sets the field separator to the semicolon character, and then iterates through the lines of data.txt, splitting each line into separate fields on the semicolon delimiter. For fields that begin with s:###:"..." (for arbitrary values of ### and ...), the script calculates the length of the quoted string, and reformats the field using that length value and adding a trailing field separator. Fields which do not begin with s:###:"..." are output verbatim, again adding back the trailing field separator.
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";};
#!/usr/bin/env bash
IFS=';'
while read LINE
do
set -- $LINE
while [ "$1" ]
do
if grep -qEx 's:[0-9]+:".*"' <<< "$1"; then
s=${1##*:}
printf 's:%d:%s%s' $((${#s}-2)) "$s" "$IFS"
else
printf '%s%s' "$1" "$IFS"
fi
shift
done
printf 'n'
done < data.txt
This script sets the field separator to the semicolon character, and then iterates through the lines of data.txt, splitting each line into separate fields on the semicolon delimiter. For fields that begin with s:###:"..." (for arbitrary values of ### and ...), the script calculates the length of the quoted string, and reformats the field using that length value and adding a trailing field separator. Fields which do not begin with s:###:"..." are output verbatim, again adding back the trailing field separator.
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";};
answered 47 mins ago
Jim L.Jim L.
61525
61525
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either[[orcase.
– icarus
42 mins ago
add a comment |
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either[[orcase.
– icarus
42 mins ago
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either
[[ or case.– icarus
42 mins ago
Using a process per line is expensive (calling grep), when bash has pattern matching built into it using either
[[ or case.– icarus
42 mins ago
add a comment |
How about
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
Ex.
$ echo 'a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}' |
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
You can add -i to perform the substitution on the file in-place.
add a comment |
How about
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
Ex.
$ echo 'a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}' |
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
You can add -i to perform the substitution on the file in-place.
add a comment |
How about
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
Ex.
$ echo 'a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}' |
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
You can add -i to perform the substitution on the file in-place.
How about
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
Ex.
$ echo 'a:7:{i:0;s:4:"9999";i:1;s:4:"10000";i:2;s:4:"10001";i:3;s:4:"10002";i:4;s:4:"10003";i:5;s:4:"10004";i:6;s:4:"989";}' |
perl -pe 's/s:d+:"(.*?)"/sprintf("s:%d:"%s"",length($1),$1)/ge'
a:7:{i:0;s:4:"9999";i:1;s:5:"10000";i:2;s:5:"10001";i:3;s:5:"10002";i:4;s:5:"10003";i:5;s:5:"10004";i:6;s:3:"989";}
You can add -i to perform the substitution on the file in-place.
answered 38 secs ago
steeldriversteeldriver
39.4k45492
39.4k45492
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f521636%2fparse-data-based-of-number-of-digits-in-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown