How to rename files by reducing the number value within filenames by the same amount for each file [closed] - awk

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 days ago.
Improve this question
I have a list of files that are of this format:
"stuff that happened B05T300 today.png"
"stuff that happened B05T301 today.png"
"stuff that happened B05T302 today.png"
"stuff that happened B05T303 today.png"
I would like to rename the files in 2 ways.
1. B05 needs to become B01
2. T300 needs to become T001, T301 -> T002, etc... essentially the Txxx will be xxx - 299.
Potentially also have the 0 chars removed immediately after the T. ie. T1, T10

Using Perl's rename:
$ rename -n 's/B05T/B01T/;s/(?<=B01T)(\d+)/sprintf "%03d", ($1 - 299)/e' *.png
rename(stuff that happened B05T300 today.png, stuff that happened B01T001 today.png)
rename(stuff that happened B05T301 today.png, stuff that happened B01T002 today.png)
rename(stuff that happened B05T302 today.png, stuff that happened B01T003 today.png)
rename(stuff that happened B05T303 today.png, stuff that happened B01T004 today.png)
Remove -n (aka dry-run) when the output looks satisfactory.

Related

grep each line from a file recursively [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an alphabetically sorted file names.txt containing names:
Dio Brando
Erina Pendleton
Jonathan Joestar
Mario Zeppeli
....
and a folder containing files:
a1.txt
a2.txt
....
zn.txt
each having lines with Name: phone number. Assuming recursion goes though files alphabetically, I'd like to grep each line from names.txt and switch to the next name whenever it fails to find a match in the next one.
For example: I grep for "Dio Brando" and it reaches in file, say, D2.txt following lines:
Dio Brando: phone number 1
Dio Brando: phone number 2
Dio Brando: phone number 3
<not Dio Brando>: phone number 1
When I hit the <not Dio Brando> line, I want it to start searching for "Erina Pendleton" where it left off (from D2.txt onward). It's guarantied to find at least one match, and when it fails after that, it should switch to "Jonathan Joestar" etc. Is it possible without some serious scripting?
If resuming with the next name is not doable, just performing grep on each line through the whole folder will do.
It sounds like this might be all you need:
awk '
BEGIN { nameNr = 1 }
NR==FNR {
names[NR] = $0
next
}
{
if ( index($0,names[nameNr]) ) {
print FILENAME, FNR, $0
found = 1
}
else if ( found ) {
nameNr++
found = 0
}
}
' *.txt
but since you didn't provide sample input/output we could test with that's just an untested guess.

Grep from specific file and print after matching strings [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to get grep matching output from 2 list files, but still print the previous information.
Here are the example files and the wanted output.
Input_1
AAAAA
CCCCC
DDDDD
EEEEE
Input_2 (tab_delimited) (there might be some blank cells)
AAAAA:1 - -
- BBBBB:0.5 -
- - CCCCC:0.2
- DDDDD:0 -
Wanted output
AAAAA:1
CCCCC:0.2
DDDDD:0
I have been trying all sorts of grep options and couldn't figure it out. Other methods (other than using grep) are all welcome!
Thank you in advance!
grep -Eof <(awk '{print $0":[0-9.]+"}' <input1>) <input2>
I use awk to change input1 and create patterns that input2 can match.

make wrapped text one line using awk [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a file with following records:
1 Error 19-03-23 02:02:26 LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of
PIC1 is abnormal[OID:1.3.6.1.4.1.201
1.5.25.129.2.1.9,BasCode:67697]
2 Error 19-03-20 07:50:40 The air filter : Maybe it is not clean
ed as scheduled. Please clean it and
run the reset dustproof run-time comman
d[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,
BasCode:67995]
I want to output:
1 Error 19-03-23 02:02:26 LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of PIC1 is abnormal[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67697]
2 Error 19-03-20 07:50:40 The air filter : Maybe it is not cleaned as scheduled. Please clean it and run the reset dustproof run-time command[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67995]
GNU sed:
sed -En '/^[[:digit:]]+[[:blank:]]+/{:l1;N;/]$/!{b l1};s/\n +//g;s/ +/ /g;s/ /\t/g;s/\t/ /5gp}' file
Output
1 Error 19-03-23 02:02:26 LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of PIC1 is abnormal[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67697]
2 Error 19-03-20 07:50:40 The air filter : Maybe it is not cleaned as scheduled. Please clean it and run the reset dustproof run-time command[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67995]
Could you please try following.
awk '
{
if($0 ~ / +$/){
sub(/ +$/," ")
}
}
FNR==1{
printf("%s",$0)
next
}
{
if($0 ~ /^ +/){
check=1
sub(/^ +/,"")
}
printf("%s%s",check==1?"":ORS,$0)
check=""
}' Input_file
following is the output I am getting.
1 Error 19-03-23 02:02:26 LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of PIC1 is abnormal[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67697]
2 Error 19-03-20 07:50:40 The air filter : Maybe it is not cleaned as scheduled. Please clean it and run the reset dustproof run-time command[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67995]

Obtain Users and Groups from Mac [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How would I obtain the Users and Groups listed under System Preferences from a Mac through the terminal?
I've tried
dscl . list /users
dscl . list /groups
But they give me a list of system-ish users as well. I just want the users and groups I would see if I went into System Preferences and viewed them.
The system treats users/groups with IDs <= 500 as "system" users, which are hidden from the UI. So you can do this:
$ for user in `dscl . list /users`; do if [ `dscl . read "/users/$user" | grep UniqueID | awk '{print $2;}'` -gt 500 ]; then echo "$user" ;fi; done
You can use dscacheutil to get those lists.
dscacheutil -q user
dscacheutil -q group

Need to Ping from one process to another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two process P1 and P2. I want to ping from P1 to P2 to tell P2
that it is alive and working. How do I do that?
On the same machine (assuming there is a file system), you could use a "heart beat file". Have P1 update the file with the current time-stamp periodically, and P2 can check if that gets too old. You can also write the P1 process id in there, and P2 can check if that pid is still alive.
You could setup a network socket listener on P2 that P1 connects to periodically. You'll need to specify the language you're using for a more specific code example.
As Thilo mentioned below, since they are on the same system you could use a simple file as the "I'm alive" message. Here's an example:
Have "P1" update the file with the current time every few seconds. Here is a shell example (sorry, I don't do C++ well):
rm /tmp/P1.heartbeat
date +%s > /tmp/P1.heartbeat
P2 then opens-reads-closes the file. It then compares it to the current time and takes the appropriate action if the time in the file is "too old".
Just make sure your P2 system closes the file after each read to make sure it re-reads the new file.
X=`cat /tmp/P1.heartbeat`
NOW=`date +%s`
DIFF=$(( $NOW - $X))
if [ $DIFF -gt 60 ] ; then
echo P1 died $DIFF seconds ago.
fi