Loop for replace string by other one in different file - android

I have two lists
list1:
A:1
B:3
C:1
D:5
list2:
1:blue
3:green
5:red
How can i do for have something like:
Desired output(file3):
A:blue
B:green
C:blue
D:red
And here is my unworking code ...
#!/system/bin/bash
list1=$(cat file1)
list2=$(cat file2)
for i in "$list1"; do
num_file1=$( echo $i | cut -d ":" -f 2)
string_file2=$(cat $list2 | grep "$num_file1" | cut -d ":" -f 2)
echo -e "$i" | sed "s/$num_file1/$string_file2/" > list3
done
I also tried
sed 's/"$num_file1"/"$string_file2"/' and many other but failed every times for what i want .. Where i am wrung with sed ??
Ps: its on android ... and few command are misted ...

Give this awk line a try:
awk -F':' 'NR==FNR{k[$1]=$2;next}{print $1 FS k[$1]}' f2 f1
I didn't test the code, but it should work.
Note this assumes that all idx in file1 we have corresponding entry in file2

The awk solution is perfectly fine. Using lesser commands, you could say:
sort -t \: -k 2 list1 | join -t \: -1 2 - list2 | cut -d \: -f 2,3 | sort -t \:

Where i am wrung with sed ??
You were quite close, wrong only in three places.
for i in "$list1"; do has to be for i in $list1; do without quotes, since you want to process the lines individually, not the whole $list1 at once.
string_file2=$(cat $list2 | …) has to be string_file2=$(echo "$list2" | …) with echo instead of cat (since you already read the file2 into the variable $list2) and with quotes to preserve the line separation.
The output redirection > list3 has to be moved away from the echo -e "$i" | sed … line to the end of the done line, otherwise only the last output line would remain in file3.
Another approach to the problem would be to use an array for the colors, indexed by their number:
#!/bin/bash
eval rgb=($(sed 's/\(.*\):/[\1]=/' <file2)) # change 1:blue to [1]=blue etc.
while IFS=: read letter number
do echo $letter:${rgb[$number]} # change A:1 to A:blue etc.
done <file1 >file3

Related

Are there way to find format errors in localized strings for translated strings android res xml?

I am working on Crowdin client js api to fetch translated strings from Crowdin. I have implemented upload of strings from res dir to crowdin, download of translated strings from Crowdin and update of localized strings to android res dir.
But the issues I am trying to fix is that how the strings format errors can be found and fixed? There are shell script to do that but it does work on plurals and some more strings.
if grep -RHn "%1$ s" res/values*; then
echo "Found '%1$ s'-related error"
EXIT_STATUS=$((EXIT_STATUS + 1));
fi
if grep -RHn "%1$ d" res/values*; then
echo "Found '%1$ s'-related error"
EXIT_STATUS=$((EXIT_STATUS + 1));
fi
if grep -RHn "%1" res/values* | grep -v "%1\\$"; then
echo "Found '%1'-related error"
EXIT_STATUS=$((EXIT_STATUS + 1));
fi
if grep -RHn '%' res/values* |
sed -e 's/%/\n%/g' | # Split lines that contain several expressions
grep '%' | # Filter out lines that do not contain expressions
grep -v ' n% ' | # Lone % character, not a variable
grep -v '(n%)' | # Lone % character, not a variable
grep -v 'n%<' | # Same, at the end of the string
grep -v '>n% ' | # Same, at the beginning of the string
grep -v '%で' | # Same, no spaces in Japanese
grep -v '%s' | # Single string variable
grep -v '%d' | # Single decimal variable
grep -v '%[0-9][0-9]\?$s' | # Multiple string variable
grep -v '%[0-9][0-9]\?$d' | # Multiple decimal variable
grep -v '%1$.1f' | # ?
grep -v '%.1f' |
grep -v '%\\n' |
then
echo "Found grep errors but if you are not on macOS they are likely false positive. Ignoring"
#EXIT_STATUS=$((EXIT_STATUS + 1))
fi
Can gradle lint be used to find errors for format in localized strings?

adb shell Logcat with Package Name

Is it possible to also display the Log's Package Name in each line?
Using
logcat -v long
leaves exactly the package name field (after PID) empty.
I want to filter the Logs from a specific application with different Tags, just wondering if it is possible.
logcat record does not have a "package name field". Therefore there is no standard/built-in way to filter by it.
Although since Android 7.0 you can use logcat --pid option combined with pidof -s command to filter output by binary/package name:
adb shell "logcat --pid=$(pidof -s <package_name>)"
Replace " with ' for Linux/MacOS
This is my script. A little rusty but still working.
PID=`adb shell ps | grep -i <your package name> | cut -c10-15`;
adb logcat | grep $PID
Actually I have a python script to add more colours, and a while loop to keep monitoring for that process when it gets restarted, but I think you'll get the point.
Option 1
Enter these line by line:
adb shell
su
bash
PKG_PID_LIST_CACHE=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
PROC_PID_LIST_CACHE=$(ps -A -o NAME -o PID)
PID_LIST_CACHE=$(echo "$PKG_PID_LIST_CACHE\n$PROC_PID_LIST_CACHE")
function pid2pkg() { pkgName=$(echo "$PID_LIST_CACHE" | grep -w $1 | cut -d ' ' -f1 | head -1); if [ "$pkgName" != "" ] ; then echo $pkgName; else echo "*NOT RUNNING*"; fi }
eval "$(logcat -d | sed -r -e 's/([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)/\2 \$\(pid2pkg \3\) \5/g' | sed -r -e 's/(.+)/echo -e \"\1\"/g')"
The logcat output will be in the following format:
[Time] [Name] [Type] [Message]
Example:
14:34:59.386 wpa_supplicant E wpa_supplicant: nl80211: Failed to set IPv4 unicast in multicast filter
14:35:02.231 com.android.phone D TelephonyProvider: subIdString = 1 subId = 1
14:35:03.469 android.hardware.wifi#1.0-service E WifiHAL : wifi_get_logger_supported_feature_set: Error -3 happened.
14:35:03.518 system_server I WifiService: getWifiApEnabledState uid=10086
14:35:03.519 dev.ukanth.ufirewall D AFWall : isWifiApEnabled is false
14:35:03.520 system_server I GnssLocationProvider: WakeLock released by handleMessage(UPDATE_NETWORK_STATE, 0, 123)
14:35:03.522 dev.ukanth.ufirewall I AFWall : Now assuming wifi connection
Some system processes don't have packages. system_server and wpa_supplicant for instance. If a package name can't be found, the process name will be displayed instead.
Caveats:
If the process behind a certain PID is not running anymore, you can't get the package/process name anymore.
After a process exited itself or your device restarted, the PIDs may actually be assigned to completely different processes.
So you might want to check for how long the process has actually been running:
ps -e -o pid -o stime -o name
Option 2
If you want the formatting to be a bit more readable, you can copy my logging script to your device and execute it:
better_logging.sh
PKG_PID_LIST_CACHE=$(eval $(pm list packages | cut -d ':' -f 2 | sed 's/^\(.*\)$/echo \"\$\(echo \1\) \$\(pidof \1\)\";/'))
PROC_PID_LIST_CACHE=$(ps -A -o NAME -o PID)
PID_LIST_CACHE=$(echo "$PKG_PID_LIST_CACHE\n$PROC_PID_LIST_CACHE")
MAX_LEN=$(echo "$PID_LIST_CACHE" | cut -d ' ' -f1 | awk '{ if ( length > L ) { L=length} }END{ print L}')
function pid2pkg() {
pkgName=$(echo "$PID_LIST_CACHE" | grep -w $1 | cut -d ' ' -f1 | head -1);
if [ "$pkgName" != "" ] ; then
printf "%-${MAX_LEN}s" "$pkgName";
else
printf "%-${MAX_LEN}s" "<UNKNOWN (NOT RUNNING)>";
fi
}
eval "$(\
logcat -d | \
sed -r -e 's/([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +(.*)/\2\ $\(pid2pkg \3\) \5/g' | \
sed -r -e 's/(.+)/echo -e \"\1\"/g' \
)" | \
awk '
function color(c,s) {
printf("\033[%dm%s\033[0m\n",90+c,s)
}
/ E / {color(1,$0);next}
/ D / {color(2,$0);next}
/ W / {color(3,$0);next}
/ I / {color(4,$0);next}
{print}
'
To copy and run it, you can use:
adb push better_logging.sh /sdcard/better_logging.sh
adb shell "bash /sdcard/better_logging.sh"
Output will look like this:
This works with awk.
adb logcat | grep -F "`adb shell ps | grep com.yourpackage | awk '{print $2;}'`"

Is this shell script correct?

I have a shell script like this :
sed -i '/^###########/,/^#End of Build.Prop/d' /system/build.prop;
#
sed -i '/^#Start Build.Prop Tweak/,/^#End of Build.Prop Tweak/d' /system/build.prop;
#
sed -i '/^#Start Build.Prop Tweak/,/^ro\.config\.hwfeature_wakeupkey=0/d' /system/build.prop;
Of the three Shell Commands stated above none of them works when put in a sh file. But, if I use a TerminalEmulator, the three scripts can be executed
I want to use the scripts in an Android Device
No, it is too dangerous.
When the end-search tag is missing, you will delete a large part of your file.
When you want to delete the first and second line in a file, it seems working ok:
$ cat test.txt
first line
second line
third line
$ cat test.txt | sed '/first/,/second/ d'
third line
EDIT: One command less with sed '/first/,/second/ d' test.txt
But what happens when the second line can not be found?
Your sed command should skip removing lines, but it will:
$ cat test.txt | sed '/first/,/mistake/ d'
$
EDIT: One command less with sed '/first/,/mistake/ d' test.txt
All lines from the first match have been deleted !

Highlight Android NDK error output

Is there tool that adds highlighting to android ndk build output (may be on stderr).
e.g. If it will highlight word "error:" in red and "Warning:" in orange that would be what I look.
But if it will also give different colors to code and error messages then it would be awesome!
ANSWER
red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)
$ANDROID_NDK/ndk-build 2>&1 | sed -e "s/\(error:\)/${red}\1${norm}/i" | sed -e "s/\(warning:\)/${yellow}\1${norm}/i"
You could do this by piping the output of your build command (or any other terminal command) to sed:
red=$(tput setaf 1)
yellow=$(tput setaf 3)
norm=$(tput sgr0)
make | sed -e "s/\b\(error:\)\b/${red}\1${norm}/i" | sed -e "s/\b\(warning:\)\b/${yellow}\1${norm}/i"

Tool to check Android strings.xml variables?

A simple typo in an Android localization variable (for instance %1d instead of %1$d in strings.xml) can lead to a crash.
We can't afford to test exhaustively (many screens, some very rarely shown, tens of languages, very frequent releases, no revenue) so we must find a smarter way. Those errors are not shown in Eclipse, and actually I am looking for a non-visual tool so that it can be called by our automatic release tool.
I wrote the following script to check the localisation files:
#! /bin/sh
# Spot malformed string replacement patterns in Android localization files.
grep -R "%1$ s" values*
grep -R "%1$ d" values*
grep -R '%' values* |
sed -e 's/%/\n%/g' | # Split lines that contain several expressions
grep '%' | # Filter out lines that do not contain expressions
grep -v ' % ' | # Lone % character, not a variable
grep -v '%<' | # Same, at the end of the string
grep -v '% ' | # Same, at the beginning of the string
grep -v '%で' | # Same, no spaces in Japanese
grep -v '%s' | # Single string variable
grep -v '%d' | # Single decimal variable
grep -v '%[0-9][0-9]\?$s' | # Multiple string variable
grep -v '%[0-9][0-9]\?$d' | # Multiple decimal variable
grep -v '%1$.1f' | # ?
grep -v '%.1f'
grep -R '%' values*
PROBLEM: It fails to catch problems in Arabic localization files.
QUESTION: rather than re-inventing the wheel, is there already such a validation tool? (not Eclipse)
If not: what checks did I forget?
Note: this script is open source
This sounds like something that should be incorporated into the Lint tool in ADT.

Categories

Resources